Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preg match text in php between html tags

Tags:

Hello I would like to use preg_match in PHP to parse the "Desired text" out of the following from a html document

<p class="review"> Desired text </p> 

Ordinarily I would use simple_html_dom for such things but on this occasion it cannot be used (the above element doesn't appear in every desired div tag so I'm forced to use this approach to keep track of exactly when it doesn't appear and then adjust my array from simple_html_dom accordingly).

Anyway, this would solve my problem.

Thanks so much.

like image 471
David Willis Avatar asked Oct 19 '09 03:10

David Willis


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 to preg_ match in PHP?

PHP preg_match() Function$str = "Visit W3Schools"; $pattern = "/w3schools/i"; echo preg_match($pattern, $str);

What does preg match return?

preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

What is meaning of preg in PHP?

Definition and Usage The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise. If the optional input parameter pattern_array is provided, then pattern_array will contain various sections of the subpatterns contained in the search pattern, if applicable.


1 Answers

preg_match("'<p class=\"review\">(.*?)</p>'si", $source, $match); if($match) echo "result=".$match[1]; 
like image 153
serg Avatar answered Sep 18 '22 14:09

serg