Can anyone explain the difference in a real life situation between strpos() and strstr()?

Can anyone explain the difference in a real life situation between strpos() and strstr()?
OctalysAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jet-blackConnect With a Mentor Commented:
You can also see by example:

$text = 'string';

The result of
echo strpos($text, 'ri');
is
2

Open in new window


On the other hand, the result of
echo strstr($text, 'ri');
is
ring

Open in new window



strpos will give you position of the the text part you search.
strstr will give you the substring of the text started by the position of the the text part you search (which is equal to strpos() func.'s value) to the final character.
0
 
jet-blackCommented:
0
 
jet-blackCommented:
So, if you only check if the text part exists in the string, just use strpos() function. It is faster.

There will be times when you will need the strstr() function too.
Here's the example code from the manual:

<?php
$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com

$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user; // prints name
?>

Open in new window

0
 
OctalysAuthor Commented:
thanks for the answer.
I was looking at the php documentation of both functions, but somehow I couldnt find the difference. Now I see it.

Make sense.

Thanks.
0
 
jet-blackCommented:
You're welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.