开发者

preg_match condition when the block will execute?

preg_match condition when the block will execute?

I have an

$A = Array (
 [0] => KSO/OCMT/GBP66401,/ 001 VTS EMIS 43628
 [1] => KSO/OCMT/GBP1836,22/ ENCT LCR 090724 
)   

$test = 'KSO';

foreach($A as $temp_indice=>$temp)
{
      if(preg_match("`(.*)".$test."(.*)`im", $temp,$matches)) 
   开发者_如何学运维    {
        //WHEN THIS BLOCK IS EXECUTE?
       }
    }

I have read preg_match not get the understand from the above code.

Could anybody here good understand about preg_match explain me?


preg_match() is about regular expressions (aka regex), its goal is to search if a string matches a specific pattern, for example check if it contains a specific word, if it's an email, a zip code, ...

Here your regex will match any string containing the string 'KSO'. Regex uses a specific syntax, that's probably why you don't understand how it works. You will find more details here : http://www.regular-expressions.info/


preg_match returns 0 if no match is found and 1 if there's a match (and stop there, use preg_match_all for more)

0 is also known as the boolean false and 1 for true.

That means, if a match is found (each time KSO found in a row of your array named $A) it'll execute the block

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜