I want a regex code , to replace all "foo" strings to "bar" , between the html tags pre>< /pre>
here is an example :
< html>
< p> blah blah blah foo try foo< /p>
< pre> foo try foo word foofoo < /pre>
< /html>
shoud be
< html>
< p> blah blah blah foo try foo< /p>
< pre> bar try bar word barbar < /pre>
< /html>
so , that's means all foo between the tags pre should be replaced by .
i tried to use this regex pattern but its not working.
do {
$string = preg_r开发者_如何学JAVAeplace('/< pre>([^)]*)foo([^)]*< /pre>)/U', '\1boo\2', $string, -1,$count);
}while($count != 0);
echo $string;
im sorry for my english Thank you,
$string = '< html>
< p> blah blah blah foo try foo< /p>
< pre> foo try foo word foofoo < /pre>
< /html>';
$string = preg_replace('/foo(?=(?:.(?!< pre>))*< \/pre>)/Um', 'boo', $string);
echo $string;
You could use DOMDocument to extract the text content of your HTML tags, do a simple str_replace on the text and update the DOM.
First, go get Simple HTML Dom
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find the first pre (you can change this or find an array based on documentation on website to suit your needs, but I will make it just for the first pre for now
$html->find('pre', 0)->plaintext = str_replace('foo', 'bar', $html->find('pre', 0)->plaintext);
//Echo HTML
echo $html;
精彩评论