PHP dize metni bul

4 Cevap php

This should be pretty straightforward, but I can't seem to find an explanation anywhere on how to do it. I have a string in PHP. That string might contain within it somewhere the substring ":ERROR:". I need to find if it has that string. strpos() has worked perfectly up to this point, until today when ":ERROR:" was the first item in the string, so strpos() returned 0, so the program kept running thinking it had no error.

Ben dizesini değiştirmek gerekmez, ya da herhangi bir manipülasyon yapmak, ben sadece basit bir true/false cevap gerekiyor "yapar: HATA:? Dize var"

4 Cevap

Dize bulunamadı zaman strpos returns false, yani false yerine örtük bir durum yapma kontrol edin.

if(strpos($string, ':ERROR:') !== false) {
    // Do something...
}

Anurag bu gibi fonksiyonları ile yorum, dedi gibi ortak bir kaynağı var, çünkü (bunun yerine sadece == operatörü bırakarak veya kullanma ===) sıkı bir karşılaştırma yapmak için her zaman en iyisidir Özellikle birçok fonksiyonu farklı değerleri döndürebilir PHP, böcek,.

if(strpos($string, ':ERROR:') !== false){
    //found ':ERROR:' in string
}

"[strpos()] [r]eturns the position as an integer. If needle is not found, strpos() will return boolean FALSE."
http://php.net/manual/en/function.strpos.php

Konumu 0 ise, o zaman sadece == kullanan ya! = 0 değerlendirmek ve eşdeğeri olarak yanlış olacaktır. Yani === kullanabilir ya! == Tip zorlama önlemek için.