I need to search inside a string to find if there ise any matches to this pattern:
class="%men%"
It means that the class may be equal to either:
class="men"
class="mainmen"
class="MyMen"
class="menSuper"
class="MyMenSuper"
etc
I need someting like strpos($string,'class="%men%')
where % could be anything.
Best, Martti
Try using preg_match
if (preg_match("/men/i", $class)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
Look at this link for more information http://www.php.net/preg_match
Edit :
So you can do like this (Inspired from the answer of Marius.C)
$string = '<div class="menlook">Some text</div>';
if (preg_match('/class="(.*)men(.*)"/i', $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
Store class "men" as string in variable like "$your_class"
then use preg_match like this:
if(preg_match('/men/i', $your_class)) { echo "Men Class Found!"; }
ref: http://php.net/preg_match
or using strpos:
if(strpos(strtolower($your_class),'men')!==false) { echo "Men Class Found!"; }
This is possible without regular expressions which are quite slow
stristr($string, 'men')
Use strpos
two times,
if(strpos($string,'class=') !== false && strpos($string,'men') !== false){
echo "true";
}
Note: strpos
is much faster than preg_match
.
I believe that you need something like this:
preg_match_all('/class="(.*)men(.*)"/i', $string, $matches);