Question

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

Was it helpful?

Solution

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.";
}

OTHER TIPS

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!"; }

ref: http://www.w3schools.com/php/func_string_strpos.asp

Use strpos two times,

if(strpos($string,'class=') !== false && strpos($string,'men') !== false){
   echo "true";
}

Note: strpos is much faster than preg_match.

This is possible without regular expressions which are quite slow

stristr($string, 'men')

I believe that you need something like this:

preg_match_all('/class="(.*)men(.*)"/i', $string, $matches);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top