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.
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.
PHP preg_match() Function$str = "Visit W3Schools"; $pattern = "/w3schools/i"; echo preg_match($pattern, $str);
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 .
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.
preg_match("'<p class=\"review\">(.*?)</p>'si", $source, $match); if($match) echo "result=".$match[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With