views:

133

answers:

2

Hello I am using preg_match to parse for data, it works 99% of the time but sometimes it gives me a result like:

$match[1] = <a href="example">text i want</a>

when what I really want is the "text i want" string. I am looping preg match and 99% of the time $match[1] gives me the text string i want but I want to implement something into the code like

if($match[1] is of the form <a href="example">blah blah</a>){
$match[1] = "blah blah" }

How would I go about achieving this? Thanks for reading.

+2  A: 

If you're trying to get data between HTML tags, use an HTML parser. Regex is NOT the way to go when parsing HTML, because it's not always consistent enough for the regex matches to be reliable.

BraedenP
I'm actually using simple_html_dom to parse the html, I am using regex to do something with the results of simple_html_dom that simple_html_dom cannot do.Like I said above it works 99% of the time, every once in a while one of the results is like the above.
David Willis
Either you're not using the dom parser's full functionality or you should use a better one. There should be no need to use regex to find the text inside a tag.
Ben James
A: 

If you want to get the text inside the html tag, try the following code.

$string = '<a href="example">text i want</a>';
$string = strip_tags($string);
echo $string; //Outputs "text i want"
pnm123