0

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).

0

3 Answers 3

1
if (strpos($a, $b) !== false) {
    // $b is in $a
} else {
    // $b is not in $a
    str_replace('Hello', $b . 'World', $b);
}
Sign up to request clarification or add additional context in comments.

Comments

0

strpos().

It returns FALSE if the substring is not found.

Comments

0

You are looking for strpos function

remember to use === instead of ==

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.