-3

I would like to check if a given string is a substring to another string. For example:

$a = 'Hello World!';
$b = 'ell';

$b is a substring of $a. Any functions that can check this?

Chris
  • 57,622
  • 19
  • 111
  • 137

3 Answers3

1

You can use strpos() function to check that

    <?php
        if (strpos('Hello World!','ell') !== false) {
        echo 'contains';
    }
   else echo 'not contains';

http://php.net/manual/en/function.strpos.php

Vidya L
  • 2,289
  • 4
  • 27
  • 42
0

Try this

$a = 'Hello World!';
$b = 'ell';
if (strpos($a, $b) !== false) {
    echo true; // In string
}
Isaac
  • 983
  • 1
  • 7
  • 13
0
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
Elasek
  • 123
  • 7