Php get html tag content

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





some content

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

Php get html tag content

Gordon

307k72 gold badges526 silver badges551 bronze badges

asked Aug 4, 2010 at 10:00

1

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:

]*'.$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("/]*id=\"content\">(.*?)<\\/div>/si", $text, $match);
$content = $match[1]; 

jim

answered Aug 4, 2010 at 10:06

jim tollanjim tollan

22.1k4 gold badges45 silver badges63 bronze badges

6

",$content);
    $comment=explode("",$comment[1]);
    var_dump(strip_tags($comment[0]));
?>

check this ,it will work for you

answered Aug 4, 2010 at 11:07

Ankur MukherjeeAnkur Mukherjee

3,7295 gold badges30 silver badges38 bronze badges

Problem is with nested divs I found solution here

contents
// where "contents" may contain nested
s. // Regex uses PCRE's recursive (?1) sub expression syntax to recurs group 1 $pattern_long = '{ # recursive regex to capture contents of "main" DIV # match the "main" class DIV opening tag ( # capture "main" DIV contents into $1 (?: # non-cap group for nesting * quantifier (?: (?!]*>|
). )++ # possessively match all non-DIV tag chars | # or ]*>(?1) # recursively match nested
xyz
)* # loop however deep as necessary ) # end group 1 capture # match the "main" class DIV closing tag }six'; // single-line (dot matches all), ignore case and free spacing modes ON // short version of same regex $pattern_short = '{((?:(?:(?!]*>|).)++|]*>(? 1))*)}si'; $matchcount = preg_match_all($pattern_long, $data, $matches); // $matchcount = preg_match_all($pattern_short, $data, $matches); echo("
\n");
if ($matchcount > 0) {
    echo("$matchcount matches found.\n");
//  print_r($matches);
    for($i = 0; $i < $matchcount; $i++) {
        echo("\nMatch #" . ($i + 1) . ":\n");
        echo($matches[1][$i]); // print 1st capture group for match number i
    }
} else {
    echo('No matches');
}
echo("\n
"); ?>

answered Feb 6, 2012 at 9:07

piernikpiernik

3,3413 gold badges39 silver badges77 bronze badges