I need help on regex or preg_match
because I am not that experienced yet with regards to those so here is my problem.
I need to get the value "get me" but I think my function has an error. The number of html tags are dynamic. It can contain many nested html tag like a bold tag. Also, the "get me" value is dynamic.
<?php function getTextBetweenTags($string, $tagname) { $pattern = "/<$tagname>(.*?)<\/$tagname>/"; preg_match($pattern, $string, $matches); return $matches[1]; } $str = '<textformat leading="2"><p align="left"><font size="10">get me</font></p></textformat>'; $txt = getTextBetweenTags($str, "font"); echo $txt; ?>
$p = $domdoc->getElementById('categories')->getElementsByTagName('p')->item(0);
The strcmp() function compares two strings. Note: The strcmp() function is binary-safe and case-sensitive. Tip: This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncmp().
Matching a string In PHP, you can use the preg_match() function to test whether a regular expression matches a specific string. Note that this function stops after the first match, so this is best suited for testing a regular expression more than extracting data.
The preg_match() function returns whether a match was found in a string.
<?php function getTextBetweenTags($string, $tagname) { $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/"; preg_match($pattern, $string, $matches); return $matches[1]; } $str = '<textformat leading="2"><p align="left"><font size="10">get me</font></p></textformat>'; $txt = getTextBetweenTags($str, "font"); echo $txt; ?>
That should do the trick
Try this
$str = '<option value="123">abc</option> <option value="123">aabbcc</option>'; preg_match_all("#<option.*?>([^<]+)</option>#", $str, $foo); print_r($foo[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