DiegoP.
DiegoP.

Reputation: 45737

PHP Check if part or the entire word is contained in the string

I have these two strings:

blablab/wp-forum.php?link

blablab/wp-forum.php

How can I check in PHP if the second one is contained in the first one or not?

I cannot find a working and easy way.

Thanks

Upvotes: 1

Views: 195

Answers (3)

Avisek Chakraborty
Avisek Chakraborty

Reputation: 8309

$str = "blablab/wp-forum.php?link";
$find = "blablab/wp-forum.php";

$position = strpos($str, $find);

if($position !== FALSE)   // !== operator to check the type as well. As, strpos returns 0, if found in first position
{
   echo "String contains searched string";
}

Upvotes: 1

jmishra
jmishra

Reputation: 2081

You can use strpos to locate the first occurence of such a string

      <?php 
      $pos = strpos($largerString,$fimdMe);
      if($pos === false) {
        // string $findMe not found in $largerString
      }
     else {
      // found it 
    }
    ?>

Upvotes: 0

sberry
sberry

Reputation: 132018

Check out the docs for the function strstr
Or strpos
Or preg_match

Upvotes: 2

Related Questions