Reputation: 4580
I need a regular expression matches any tags have classname "share"
I think I'm very close. with this:
class=".*share.*"
I want it to match these:
<div class="share"></div>
<div class="sdfsd share sdfsdfsdf"></div>
But not these:
<div class="sdfsd dfdgdg" share></div>
<a class="icon-share export-to-csv-button"/>
<a class="fxac link" href="/share"/>
Please visit: https://regex101.com/r/uU6dU0/2
Upvotes: 3
Views: 12820
Reputation: 4951
Use this regex:
class=([^=]*)([^(a-z|A-Z|0-9|\-|_)])share("|([^(a-z|A-Z|0-9|\-|_)]).*")
https://regex101.com/r/uU6dU0/4
Edit: This one is easier and will not match multiple tags:
class=("|"([^"]*)\s)share("|\s([^"]*)")
https://regex101.com/r/uU6dU0/5
Edit 2: an improved version that finds classes where single quotes are used on either side:
class=(("|')|("|')([^"']*)\s)top-menu(("|')|\s([^"']*)("|'))
Upvotes: 3
Reputation: 15301
Like I posted in my comment, you don't want to parse html with a regex. There are VERY few cases where you should. Typically you would use DOMDocument and XPath to query (similar to css) for elements. This will allow you to get the inner text, nested elements and more that regular expressions just can't do well/easily.
However, if you need to, this should work:
<?php
$text =<<<HTML
<div class="share"></div>
<div class="sdfsd share sdfsdfsdf"></div>
<div class="sdfsd dfdgdg" share></div>
<a class="icon-share export-to-csv-button"
<a class="fxac link" href="/share "
HTML;
preg_match_all('/<[^>]*class="[^"]*\bshare\b[^"]*"[^>]*>/i', $text, $matches);
echo '<pre>'.htmlentities(print_r($matches,1)).'</pre>';
Outputs:
Array
(
[0] => Array
(
[0] => <div class="share">
[1] => <div class="sdfsd share sdfsdfsdf">
)
)
which you can see in action here: http://codepad.viper-7.com/UjBvT8
Upvotes: 2