Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching everything between html <body> tags using PHP

I have a script that returns the following in a variable called $content

<body>
<p><span class=\"c-sc\">dgdfgdf</span></p>
</body>

I however need to place everything between the body tag inside an array called matches

I do the following to match the stuff between the body tag

preg_match('/<body>(.*)<\/body>/',$content,$matches);

but the $mathces array is empty, how could I get it to return everything inside the body tag

like image 713
Elitmiar Avatar asked Feb 02 '10 08:02

Elitmiar


People also ask

How to extract content between HTML tags in PHP?

preg_match() function is the easiest way to extract text between HTML tags with REGEX in PHP. If you want to get content between tags, use regular expressions with the preg_match() function in PHP. You can also extract the content inside the element based on the class name or ID.

How will get all the matching tags in a HTML file?

If you want to find all HTML elements that match a specified CSS selector (id, class names, types, attributes, values of attributes, etc), use the querySelectorAll() method. This example returns a list of all <p> elements with class="intro" .

How to get content between tags REGEX?

You can use "<pre>(. *?) </pre>" , (replacing pre with whatever text you want) and extract the first group (for more specific instructions specify a language) but this assumes the simplistic notion that you have very simple and valid HTML.

What is the use of Preg_match in PHP?

The preg_match() function returns whether a match was found in a string.


1 Answers

Don't try to process html with regular expressions! Use PHP's builtin parser instead:

$dom = new DOMDocument;
$dom->loadHTML($string);
$bodies = $dom->getElementsByTagName('body');
assert($bodies->length === 1);
$body = $bodies->item(0);
for ($i = 0; $i < $body->children->length; $i++) {
    $body->remove($body->children->item($i));
}
$string = $dom->saveHTML();
like image 124
soulmerge Avatar answered Nov 15 '22 04:11

soulmerge