chris_161 Posted July 8, 2010 Share Posted July 8, 2010 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> Link to comment https://forums.phpfreaks.com/topic/207113-php-extracting-just-tags-from-a-html-file/ Share on other sites More sharing options...
freelance84 Posted July 8, 2010 Share Posted July 8, 2010 If you want to delete at the <p>, think about it like: Replace the <p> with nothing Then you can you http://php.net/manual/en/function.str-replace.php Link to comment https://forums.phpfreaks.com/topic/207113-php-extracting-just-tags-from-a-html-file/#findComment-1082933 Share on other sites More sharing options...
hardanro Posted July 8, 2010 Share Posted July 8, 2010 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) Link to comment https://forums.phpfreaks.com/topic/207113-php-extracting-just-tags-from-a-html-file/#findComment-1082951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.