Search code examples
phppreg-match

php search within string for word?


I'm using PHP's preg_match to help determine the value of a string.
But the code only ever prints 1 or 2.

Why aren't the last two cases of my if statement ever matched?

$atype = strtolower($userData['user_type']);   // let say data is :: company introducer

if ($atype == "individual introducer" || $atype == "individualintroducer" || 
    (preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ) {

       $atype = 1 ;

} elseif ($atype == "individual investor" || $atype == "individualinvestor" ||         
          (preg_match('/i/',$atype) AND preg_match('/inv/',$atype)) ) {

       $atype = 2;

} elseif ($atype == "company introducer" || $atype == "companyintroducer" || 
          (preg_match('/c/',$atype) AND preg_match('/int/',$atype)) ){

       $atype = 3;

} elseif ($atype == "company investor" || $atype == "companyinvestor" || 
          (preg_match('/c/',$atype) AND preg_match('/inv/',$atype)) ){

       $atype = 4;

}

echo $atype;

Solution

  • You need to explain your question in a better way.

    But i guess as you say the data assumed is company introducer.

    So it already matches condition for the first if block. For ex:

    In company introducer The preg_match will return true.

    if($atype == "individual introducer" || $atype == "individualintroducer" || (preg_match('/i/',$atype) AND preg_match('/int/',$atype))  ){
        $atype =1 ;
    }