Как я проверяю в PHP, содержит ли строка конкретные вещи или нет? - PullRequest
1 голос
/ 21 июля 2011

Мне нужна функция в php, которая будет работать таким образом.

$string = "blabla/store/home/blahblah";

If in $string you find /store/ then do this, else do that.

Как я могу это сделать?

Спасибо!

Ответы [ 5 ]

5 голосов
/ 21 июля 2011

вам нужна функция strpos ()

3 голосов
/ 21 июля 2011
$string = "blabla/store/home/blahblah";
if (preg_match("|/store/|", $string)){
    //do this
}
else{
    //do that
}

или

$string = "blabla/store/home/blahblah";
if (false !== strpos($string, "/store")){
   //do this
}
else{
    //do that
}
2 голосов
/ 21 июля 2011
if (strpos($string, "/store/") !== false) {
    // found
} else {
    // not found
}
1 голос
/ 21 июля 2011

Похоже, вы ищете функцию stristr () .

$string = "blabla/store/home/blahblah";
if(stristr($string, "/store/")) { do_something(); }
1 голос
/ 21 июля 2011

Попробуйте использовать функцию strrpos

, например

$pos = strrpos($yourstring, "b");
if ($pos === true) { // note: three equal signs
//string found...
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...