Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get content within a html tag using php and replace it after processing

Tags:

html

php

get

I have an html (sample.html) like this:

<html>
<head>
</head>
<body>
<div id="content">
<!--content-->

<p>some content</p>

<!--content-->
</div>
</body>
</html>

How do i get the content part that is between the 2 html comment '<!--content-->' using php? I want to get that, do some processing and place it back, so i have to get and put! Is it possible?

like image 269
esafwan Avatar asked Nov 27 '22 12:11

esafwan


2 Answers

esafwan - you could use a regex expression to extract the content between the div (of a certain id).

I've done this for image tags before, so the same rules apply. i'll look out the code and update the message in a bit.

[update] try this:

<?php
    function get_tag( $attr, $value, $xml ) {

        $attr = preg_quote($attr);
        $value = preg_quote($value);

        $tag_regex = '/<div[^>]*'.$attr.'="'.$value.'">(.*?)<\\/div>/si';

        preg_match($tag_regex,
        $xml,
        $matches);
        return $matches[1];
    }

    $yourentirehtml = file_get_contents("test.html");
    $extract = get_tag('id', 'content', $yourentirehtml);
    echo $extract;
?>

or more simply:

preg_match("/<div[^>]*id=\"content\">(.*?)<\\/div>/si", $text, $match);
$content = $match[1]; 

jim

like image 168
jim tollan Avatar answered Dec 06 '22 01:12

jim tollan


If this is a simple replacement that does not involve parsing of the actual HTML document, you may use a Regular Expression or even just str_replace for this. But generally, it is not a advisable to use Regex for HTML because HTML is not regular and coming up with reliable patterns can quickly become a nightmare.

The right way to parse HTML in PHP is to use a parsing library that actually knows how to make sense of HTML documents. Your best native bet would be DOM but PHP has a number of other native XML extensions you can use and there is also a number of third party libraries like phpQuery, Zend_Dom, QueryPath and FluentDom.

If you use the search function, you will see that this topic has been covered extensively and you should have no problems finding examples that show how to solve your question.

like image 26
Gordon Avatar answered Dec 06 '22 00:12

Gordon