How can I grab this with RegEx?
Say I have this:
<li class="one"><strong>String here: </strong><span class="one">
<!--googleoff: all-->
<strong>STRING TO GRAB</strong>
开发者_C百科 <!--googleon: all-->
</span></li>
How can I grab the STRING TO GRAB
efficiently with RegEx? Keep in mind that this isn't the only text on the page, so /<strong>(.*)<\/strong>/
wouldn't work.
Thanks
There are two ways.
Dom classes: use the dom classes of PHP if the html is sort of a decent kind.
See: - http://www.php.net/manual/en/domxpath.query.php - http://www.php.net/manual/en/domdocument.loadhtml.php
Regex If it's not really valid html or dom loading does not work, perhaps regex is a good solution.
I'm assuming that the <!--googleoff: all--> is always present, this might work, if not, perhaps you can supply some more comments on the specificity of the string:
$string = "yourhtmlstring";
$matches = array();
preg_match('/<!--googleoff: all-->\s+?<strong>(.+)<\/strong>\s+?<!--googleon: all-->/', $string, $matches)
var_dump($matches);
Final tip To test the regex further: http://tinyurl.com/6gy6584
As said on the other answer, regex isn't the best answer for html (or xml)
/<strong>(.+?)<\/strong>/
Note the ?
which makes the regex non greedy
精彩评论