0

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_replace('/< pre>([^)]*)foo([^)]*< /pre>)/U', '\1boo\2', $string, -1,$count);
}while($count != 0);
echo $string;

im sorry for my english Thank you,

7
  • 8
    I wish Stack Overflow had a filter that banned questions containing "HTML" and "regex" in the same sentence. Commented May 14, 2011 at 7:48
  • 1
    @BoltClock Or at least a warning poping up when asking a question. Seriously. Commented May 14, 2011 at 7:49
  • "I want a code" is not how this site works. What have you tried so far? Commented May 14, 2011 at 7:50
  • @BoltClock, would you use a Regex for that kind of filters? Commented May 14, 2011 at 7:50
  • i have a code , i tried but it not working > Commented May 14, 2011 at 7:51

3 Answers 3

5
$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;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

sorry, i want to use only regex pattern to replace all, thanks for your answer .
@Programmer why? That's like insisting to use a sharpened spoon instead of a scalpell for surgery. Might work in some cases, but it's still the wrong tool for the job. See Best methods to parse HTML
@Gordon I think a better analogy would be using a scalpell to cut your bread instead of a bread knife. Yeah, sure, you can do it, but to do it properly it will take forever! And you will end up ripping a bunch of the bread (image thats your hair) wondering why its not working right. Or you could just use DOM and str_replace and problem solved, concise and understandable!
1

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;

1 Comment

Suggested third party alternatives to SimpleHtmlDom that actually use DOM instead of String Parsing: phpQuery, Zend_Dom, QueryPath and FluentDom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.