Sateesh
Sateesh

Reputation: 11

How to check the sub string is exists or not in main string

Is there any way to check the sub string is exists in main string or not. If exits, no need any another action. If not exists, I want to add some another string to main string.

For an example:

$a = 'Hello world';
$b = 'Hello';

I want to check the $b is exists in $a or not. If exists, i am sending that variable to database. If not exists, I want to use str_replace('Hello', $b.'World', $b).

Upvotes: 0

Views: 1506

Answers (3)

Dave Child
Dave Child

Reputation: 7881

if (strpos($a, $b) !== false) {
    // $b is in $a
} else {
    // $b is not in $a
    str_replace('Hello', $b . 'World', $b);
}

Upvotes: 1

mkk
mkk

Reputation: 7693

You are looking for strpos function

remember to use === instead of ==

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272497

strpos().

It returns FALSE if the substring is not found.

Upvotes: 0

Related Questions