DiegoP.
DiegoP.

Reputation: 45737

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.

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

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

How can I do it?

Thanks!

Upvotes: 1

Views: 206

Answers (5)

genesis
genesis

Reputation: 50976

$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
}

Upvotes: 3

fardjad
fardjad

Reputation: 20404

if (strpos($string, "/store/") !== false) {
    // found
} else {
    // not found
}

Upvotes: 2

Patrick Perini
Patrick Perini

Reputation: 22633

Seems like you're looking for the stristr() function.

$string = "blabla/store/home/blahblah";
if(stristr($string, "/store/")) { do_something(); }

Upvotes: 1

Francis Gilbert
Francis Gilbert

Reputation: 3442

Try using the strrpos function

e.g.

$pos = strrpos($yourstring, "b");
if ($pos === true) { // note: three equal signs
//string found...
}

Upvotes: 1

Cystack
Cystack

Reputation: 3561

you're looking for strpos() function

Upvotes: 4

Related Questions