Forum Moderators: coopster

Message Too Old, No Replies

PHP - Regex & preg replace

Grouping and back reference issue

         

tomda

11:20 am on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have the following pattern

$pattern = '/<img[^>]*?src="'.$my_url_regex.'([^"]*)-([\d]{1,4})x([\d]{1,4})\.([^"]{1,3})"[^>]*?>/i';

This pattern will match the following:
<img src="my_url/my_image-100x100.jpg">
<img src="my_url/my_image-50x100.jpg">

The replacement below just add an anchor around the img:
$replace = '<a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'attachment/$1/"><img src="'.$my_url.'$1-$2x$3.$4"></a>';

But my problem is that it doesn't match the following
<img src="my_url/my_image.jpg"> (that is without -100x100)

*************************

I tried to add an alternation mark (¦) to the pattern to accept both img urls

$pattern = '/<img[^>]*?src="'.$my_url_regex.'(([^"]*)¦(([^"]*)-([\d]{1,4})x([\d]{1,4})))\.([^"]{1,3})"[^>]*?>/i';

but then what about the back references (since $2, $3 do not exists in second case and therefore $4 become $2)

$replace = '<a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'attachment/$1/"><img src="'.$my_url.'$1(-$2x$3).$4"></a>';

I hope you understand me

Thx

idfer

3:53 pm on May 6, 2009 (gmt 0)

10+ Year Member



Looks like you don't really to modify the -100x100 part of the filename in your replacement, you just want to extract the filename minus the -100x100.jpg part? So you could simplify the pattern to:

$pattern = '/<img[^>]*?src="'.$my_url_regex.'([^"]*?)(-\d{1,4}x\d{1,4})?\.[^"]{1,3}".*?>/i

$replace = '<a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'attachment/$1/">$0</a>';

The ? after (-\d{1,4}x\d{1,4}) says that subpattern is optional. BTW, if you know the text is well-formed, you could further simplify the pattern by removing the [^"] constructs:

$pattern = '/<img[^>]*?src="'.$my_url_regex.'(.*?)(-\d{1,4}x\d{1,4})?\.\w+".*?>/i

Hope this helps

tomda

8:09 am on May 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Idfer !

Indeed, it helps !

This function will rewrite IMG in Wordpress post so that images can be seen in their own AJAX/DHTML window for JS enable user.


// WILL FIX THE HTML LINK WHEN READING CONTENT
function kijana_fix_img_link($content='') {
global $kijana_url_regex, $kijana_url;
$link = get_permalink();

$pattern = '/<img[^>]*?class="([^"]*wp-image-[\d]{1,10})"[^>]*?title="([^"]*)"[^>]*?src="'.$kijana_url_regex.'([^"]*?)(-\d{1,4}x\d{1,4})?\.(gif¦png¦jpg¦jpeg)"[^>]*?>/i';

$replace = '<a href="'.$link.'attachment/$3/" title="$2" onclick="openmypage(\''.str_replace('wp-content/', '', $kijana_url).'?type=pic&amp;url=$3.$5\', \'$2\', \'width=560px,height=560px,left=3px,top=3px,resize=1,scrolling=1\'); return false">$0</a>';

$result = preg_replace($pattern, $replace, $content);
return $result;
}
add_filter('the_content', kijana_fix_img_link);

Just one question, what do you mean by well-formed ?

Again, thank you so much

idfer

5:22 pm on May 7, 2009 (gmt 0)

10+ Year Member



By well-formed, i just meant that you don't have to deal with bad strings like <img src="my_url/my_>image.jpg">. Glad to hear it worked out for you.