Cannot replace keyword using PHP Regex -


i want replace 'keyword' in <img ..keyword../> using php regular expressions.

$content = '&lt;img alt="hello" src="http://frbird.qiniudn.com/topic/150609/5576a8837fd32e3b4ece5f6b-hd.jpg"&gt;';  $content = preg_replace('/(&lt;img\s(?!&gt;)*?)(hello)((?!&gt;)*&gt;)/u', '$1%&&&&&%$3', $content); 

i can't replace 'hello'. please tell me wrong.

first translate &lt; , &gt; tokens source string < , >. way far easier handle. preg_replace() , translate them again in target string.

$src= strtr('&lt;img alt="hello" src="http://frbird.qiniudn.com/topic/150609/5576a8837fd32e3b4ece5f6b-hd.jpg"&gt;',array('&lt;'=>'<','&gt;'=>'>')); $trg = strtr(preg_replace('/(<img[^>]*")(hello)(".*)/', '$1xxxxx$3', $src),array('<'=>'&lt;','>'=>'&gt;')); echo $trg; 

not elegant trick.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -