Cannot replace keyword using PHP Regex -
i want replace 'keyword' in <img ..keyword../> using php regular expressions.
$content = '<img alt="hello" src="http://frbird.qiniudn.com/topic/150609/5576a8837fd32e3b4ece5f6b-hd.jpg">'; $content = preg_replace('/(<img\s(?!>)*?)(hello)((?!>)*>)/u', '$1%&&&&&%$3', $content);
i can't replace 'hello'. please tell me wrong.
first translate <
, >
tokens source string <
, >
. way far easier handle. preg_replace()
, translate them again in target string.
$src= strtr('<img alt="hello" src="http://frbird.qiniudn.com/topic/150609/5576a8837fd32e3b4ece5f6b-hd.jpg">',array('<'=>'<','>'=>'>')); $trg = strtr(preg_replace('/(<img[^>]*")(hello)(".*)/', '$1xxxxx$3', $src),array('<'=>'<','>'=>'>')); echo $trg;
not elegant trick.
Comments
Post a Comment