Solved

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

Posted on 2011-09-17
5
297 Views
Last Modified: 2012-05-12
Can anyone explain the difference in a real life situation between strpos() and strstr()?
0
Comment
Question by:Octalys
  • 4
5 Comments
 
LVL 12

Expert Comment

by:jet-black
ID: 36553381
0
 
LVL 12

Accepted Solution

by:
jet-black earned 500 total points
ID: 36553395
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
 
LVL 12

Expert Comment

by:jet-black
ID: 36553413
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
 

Author Closing Comment

by:Octalys
ID: 36553706
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
 
LVL 12

Expert Comment

by:jet-black
ID: 36553713
You're welcome.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now