tags:

views:

278

answers:

6

Hey all!

I was wondering of a solid way to find phrases/words that are part of an HTML document. For example if I have the following document:

<a href="#">This is a test</a><b>Another test</b>

My goal is to find "This is a test" and "Another test" and replace it with something else. Note that these are sample phrases and it could contain numbers or the ampersand symbol.

Any help would be great.

Thank you

A: 

you could use php's strip_tags($string, $tagsToRemove)

$justText = strip_tags('<a href="#">This is a test</a><b>Another test</b>');

And then you'd have the text, so you could use str_replace("new text", $justText);

You might have to break it up using the second parameter of strip_tags() to keep the tags seperate, though.

$html = '<a href="#">This is a test</a><b>Another test</b>';
$anchorText = strip_tags($html, '<a>');
$paraText = strip_tags($html, '<p>');
$html = str_replace("new anchor text", $anchorText);
$html = str_replace("new paragraph text", $paraText);
peirix
I think he's looking to replace the text not strip the tag
Phill Pafford
+1 with Phill Pafford, and after using strip_tag it could be hard to unstrip it, as some char in the plain text might become "<" and ">" symbols.
Clement Herreman
strip_tags wouldn't make `<` and `>` characters magically appear, you know.
nickf
but stripping the tags will make it easier to replace the text, as he would then have the strings, which he could use `str_replace` against...
peirix
The thing is I need the HTML tags in place. The goal is to replace all the phrases in my HTML templates with a Smarty tag that will create phrases for a language package, so I am just trying to find the phrases mixed between all the HTML and replace it with a Smarty tag that will look like: {phrase var='name_of_phrase'}. Hope that helps.
Patrik Johansson
if you know what the phrases inside the tags will be, then you'd be fine just using `str_replace` as suggested by just about everyone. But if you don't then you could use my suggestion to find the phrases first.
peirix
+2  A: 

Consider your HTML as XML and use the DOM (PHP 5) or DOM XML (PHP 4) extension (or any other XML extension included in PHP).

For each node, you can get the inside text using DomNode.GetValue (depending on what library you use).

Clement Herreman
A: 

I would look into something like str_replace()

Phill Pafford
A: 

Here is explained how to remove all html stuff (html tags, scripts, css) and then with str_replace you can replace whatever you want.

dbrmr
A: 

If this is an option to do client side I would suggest jQuery replaceWith()

Phill Pafford
A: 

The key here is to use a regular expression to, in a sense, parse the HTML...

So you'd use:

<?php

$str = "<a href =\"\">Hello</a>"; //The string to search

preg_match('/(<.+>)??.+(<\/.+>)??/i',$str,$match); //Find all occurences and store the tag content in an array called $match

echo $match[0]; //Echo the first value

?>

This basically searches the input string (which you'd set as your page's HTML) and returns each match of text between the tags as a value in the array. For for the first tag, the value would be stored in $match[0], the second in $match[1], etc.

It does this by first finding a pattern that starts with an HTML tag and ends with an HTML tag, but not selecting either tag, leaving only the content in between selected.

Hope this helps!

Braeden

BraedenP
the key in parsing HTML is actually *not* to use regular expressions, but a proper HTML/XML parser.
ax
But if he's just getting tag content, why have the overhead of loading the parser when you could just use a simple regex?
BraedenP
because it's not a simple regex. try finding a working one for his example (yours doesn't), and then find one for '<a href="" title="test >>> sdfsfd">This is <b>Another test</b> a test</a>'.
ax
I strongly disapprove regex. What if there are "<" in attributes name ? What if it is HTML instead of xHTML ? or bad imbricated tag ? etc. Too much for regex, or at least for one or human-understandable regexs ^^
Clement Herreman