How I check in PHP if a string contains or not specific things?
I need a function in php that will work in this way.
$str开发者_运维技巧ing = "blabla/store/home/blahblah";
If in $string you find /store/ then do this, else do that.
How can I do it?
Thanks!
you're looking for strpos() function
$string = "blabla/store/home/blahblah";
if (preg_match("|/store/|", $string)){
//do this
}
else{
//do that
}
or
$string = "blabla/store/home/blahblah";
if (false !== strpos($string, "/store")){
//do this
}
else{
//do that
}
if (strpos($string, "/store/") !== false) {
// found
} else {
// not found
}
Try using the strrpos function
e.g.
$pos = strrpos($yourstring, "b");
if ($pos === true) { // note: three equal signs
//string found...
}
Seems like you're looking for the stristr() function.
$string = "blabla/store/home/blahblah";
if(stristr($string, "/store/")) { do_something(); }
精彩评论