Get all tags from a string in a list?

I’m trying to figure out if its possible to get all html tags from a string printed out in a list. I have tried a little bit with strip_tags but can only remove tags with this… can anyone guide me or tell me how to do this?

At ease

Mansa

I guess preg_match_all is the way to go.
And googling for ‘regex html tags’ I found this site: http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx

It’s a bit old, but I tested the regex given and it seems to work. At least it’s a starting point.


$results = preg_match_all('$</?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|\\'.*?\\'|[^'">\\s]+))?)+\\s*|\\s*)/?>$', '<b>example: </b><div align=\\'left\\'>this is a test</div>', $arr, PREG_PATTERN_ORDER);
print_r($results);

Thanks, but something is wrong with the code you attached?

thasa mouthful, guido.

Not exactly sure what/how you want to capture, but the simplified version:
~<([^/][^>]*?)>~

(A <, not a slash (so ignore the end-of-container tags), then anything that isnt a >, then a >).

Stucked with this.

$results = preg_match_all('~<([^/][^>]*?)>~', '<b>example: </b><div align=\\'left\\'>this is a test</div><a></a><div></div>', $arr, PREG_PATTERN_ORDER);
print_r($results);

This works but gives out the number of how many html tags is in the string. What i want is to echo the divs it finds in a list and not duplicating if a html tag is used twice…

Me? Nothing :smiley:
Did you take a look at the page I linked to? It’s all explained there (more or less). I’m not saying it can’t be done simpler, but there is a reason for some of the complexity. The example given is

<img title="displays >" src="big.gif">

Something? Could you be a little more precise?

What do you mean by that? What does the print_r($results) give you? And what is it you would like to get?

The print gives me the number or amount of html tags in the string. I would like to get tags out.

As now it prints “4”.
I would like it to print: <b>, <div align=“left”>, <a>, <div>

Hope this make sense…

You can use http://www.php.net/manual/en/domdocument.loadhtml.php, to do what you’re trying to do.

Ah yes, if you read the manual I linked to, you’ll understand why. Forgot all about that. The function returns the number of full pattern matches. The matches themselves are return in $arr


$results = preg_match_all('~<([^/][^>]*?)>~', '<b>example: </b><div align=\\'left\\'>this is a test</div><a></a><div></div>', $arr); 
echo 'number of matches : ' . $results . '<br />';
echo 'matches : '; print_r($arr); 

Oh yes… Thanks a lot…