Portal Home > Knowledgebase > Articles Database > PHP replace words in HTML output
PHP replace words in HTML output
Posted by netfreak, 10-20-2012, 08:05 AM |
How would I go about replacing each word in an HTML output that doesn't fall within an HTML tag? Basically I'm looking to modify only standard text within a variable containing a full HTML page output so the browser can still parse the page correctly. The input needs to be any word not falling within an HTML tag, and the output is going to be the same word replacing each from the input.
Using the following got me close, but it also modified the actual HTML tags:
preg_replace('/(? |
Posted by Herasil, 10-20-2012, 10:07 AM |
Maybe it's just me, but I'm kind of confused about what you're trying to do. I understood until you said "output is going to be the same word replacing each from the input," not sure what you mean by that. Can you show us a sample input and output?
|
Posted by netfreak, 10-20-2012, 10:27 AM |
Actually I managed to get this going. Essentially I have a "proxy" script that grabs a site using curl and displays it, but I wanted to replace all words not inside html tags with a single word like "hello"
Some examples I found online would do it but insert my replacement string between tags that had no existing text. Adding a \b to the expression seemed to work:
preg_replace("/(?<=>)\b[^>(.*?)<]+?(?=<)/is", "hello", $html)
|
Add to Favourites Print this Article
Also Read