note 55485 deleted from function.preg-split by tularis
| From: | tularis@php.net | Date: | Thu, 03 May 2007 22:25:42 +0000 |
| Subject: | note 55485 deleted from function.preg-split by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126178@lists.php.net to get a copy of this message | ||
Note Submitter: Jappie
----
The following pattern will match a html tag:
<?php
$pattern =
'/(<(?:[^<>]+(?:"[^"]*"|\\'[^\\']*\\')?)+>)/';
?>
So the following will nicely convert a string of html to an array, where each array-item is 1 tag or
text within a tag:
<?php
$html_string = '<html><body><p class="a<weird>name">The
classname is not seen as a different tag</p></body></html>';
$html_array = preg_split
('/(<(?:[^<>]+(?:"[^"]*"|\\'[^\\']*\\')?)+>)/',
trim ($html), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
?>
Array
(
[0] => <html>
[1] => <body>
[2] => <p class="a<weird>name">
[3] => The classname is not seen as a different tag
[4] => </p>
[5] => </body>
[6] => </html>
)