-3

I want to 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 what is wrong.

alex
  • 6,818
  • 9
  • 52
  • 103

2 Answers2

0

Don't try to handle HTML (even escaped as it is) using a RegEx. Examples: Can you provide some examples of why it is hard to parse XML and HTML with a regex?

Parse the fragment using SimpleXML/DOM, and look for attributes that way.

<?php
$string = html_entity_decode('&lt;img alt="hello" src="http://frbird.qiniudn.com/topic/150609/5576a8837fd32e3b4ece5f6b-hd.jpg"&gt;');

$dom = new DOMDocument();

$dom->loadHTML(
    $string,
    LIBXML_COMPACT | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
);

$attr = $dom->documentElement->removeAttribute('alt');

echo $dom->saveHTML();
Community
  • 1
  • 1
Stephen
  • 18,597
  • 4
  • 32
  • 33
0

First translate the &lt; and &gt; tokens from the source string into < and >. That way they are far easier to handle. Then do the preg_replace() and translate them back again in the 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 very elegant but it will do the trick.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43