Possible Duplicate:
Which method is preferred strstr or strpos ?
Hi!
Could you tell me which one is faster:strstr($mystring, $findme);
ORstrpos($mystring, $findme);
OR
anything else
in finding the - first or any - occurrence of a string in another one?
Does it even matter in performance if I check the occurrence in a case-insensitive mode with stristr()
OR stripos()
?
In my case it doesn't matter in which exact position the given string is (if any), or how many times it occurs in the other one (if any), the only important question is if it even exists in the other string.
I've already found some comments about differences of speed in various articles (e.g. on php.net, someone says strstr() is faster in case there is a !== false check after strpos), but now I can't decide which is true.
If you know about any better methods of searching a string in another, please let me know!
Thank you very much for the relevant comments!
============
$mystring = 'blahblahblah';
$findme = 'bla';
if(strstr($mystring, $findme)){
echo 'got it';
}
else{
echo 'none';
}
echo PHP_EOL;
if(strpos($mystring, $findme) !== false){
echo 'got it';
}
else{
echo 'none';
}
To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.
The count() method returns the number of occurrences of a substring in the given string.
Define a string and take the string as input form the user. Two loops will be used to find the duplicate characters. Outer loop will be used to select a character and then initialize variable count by 1 its inside the outer loop so that the count is updated to 1 for every new character.
strpos
seems to be in the lead, I've tested it with finding some strings in 'The quick brown fox jumps over the lazy dog'
:
strstr
used 0.48487210273743 seconds for 1000000 iterations finding 'quick'
strpos
used 0.40836095809937 seconds for 1000000 iterations finding 'quick'
strstr
used 0.45261287689209 seconds for 1000000 iterations finding 'dog'
strpos
used 0.39890813827515 seconds for 1000000 iterations finding 'dog'
<?php
$haystack = 'The quick brown fox jumps over the lazy dog';
$needle = 'quick';
$iter = 1000000;
$start = microtime(true);
for ($i = 0; $i < $iter; $i++) {
strstr($haystack, $needle);
}
$duration = microtime(true) - $start;
echo "<br/>strstr used $duration microseconds for $iter iterations finding 'quick' in 'The quick brown fox jumps over the lazy dog'";
$start = microtime(true);
for ($i = 0; $i < $iter; $i++) {
strpos($haystack, $needle);
}
$duration = microtime(true) - $start;
echo "<br/>strpos used $duration microseconds for $iter iterations finding 'quick' in 'The quick brown fox jumps over the lazy dog'";
$needle = 'dog';
$start = microtime(true);
for ($i = 0; $i < $iter; $i++) {
strstr($haystack, $needle);
}
$duration = microtime(true) - $start;
echo "<br/>strstr used $duration microseconds for $iter iterations finding 'dog' in 'The quick brown fox jumps over the lazy dog'";
$start = microtime(true);
for ($i = 0; $i < $iter; $i++) {
strpos($haystack, $needle);
}
$duration = microtime(true) - $start;
echo "<br/>strpos used $duration microseconds for $iter iterations finding 'dog' in 'The quick brown fox jumps over the lazy dog'";
?>
From the PHP Docs:
Note:
If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.
I'm willing to take their word for it :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With