Forum Moderators: coopster
$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
$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
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&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