Jump to content

Php extracting just <p> tags from a html file


chris_161

Recommended Posts

Hi there, below is the code i have done so far, im struggling to get to grips with how i can extract just the <p> tags from the html file simple. As things stands it just displays the whole site, whilst i want it to display just the information within the <p> tags of the site. Any help be much appricated. Thanks in advance.

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<?php require("simple.html");

$ptag = '';

echo strip_tags($ptag);

echo "\n";

 

// Allow <p> tags

echo strip_tags($ptag, '<p>');

?>

<body>

</body>

</html>

No sure if i understand corectly, but if you want to get only what is in your <p> tags you must use something like:

$content = file_get_contents('simple.html');
$pattern = '/(<(p)[^>]*>)(.*?)(<\/p>)/i';
preg_match_all($pattern,$content,$pTags);

//$pTags[0] will contain <p> content <p>...</p> tags enclosed
//$pTags[3] will contain what is inside  <p>...</p> tags (without <p> and </p> markup)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.