Daniel Brown wrote:
On Mon, Apr 7, 2008 at 9:25 AM, John Taylor-Johnston
<jt.johnston@xxxxxxxxxxxxxx> wrote:
$name = "John Taylor";
I want to verify if $name contains "john", if yes echo "found";
Cannot remember which to use:
http://ca.php.net/manual/en/ref.strings.php
Sorry,
John
<?php
if(stristr($name,'john')) {
// True
}
?>
Since you said you wanted to know if it "contains 'john'", this
will find if 'john' (insensitive) matches any part of the name. So in
your case, it would match on both John and Johnston.
I wonder if using strstr() with strtolower() would be faster or slower.
<?php
$max = 10;
$it = 0;
while ( $it++ < $max ) {
echo "Attempt #{$it}<br />";
$string = 'John Doe';
$counter = 10000;
$new_string = strtolower($string);
$start_time_1 = microtime(true);
while ( $counter-- ) {
if ( strstr($new_string, 'john') ) {
### Found it
}
}
echo microtime(true) - $start_time_1."<br />";
$counter = 10000;
$start_time = microtime(true);
while ( $counter-- ) {
if ( stristr($string, 'john') ) {
### Found it
}
}
echo microtime(true) - $start_time."<br />";
}
?>
Results:
Attempt #1
0.015728950500488
0.022881031036377
Attempt #2
0.015363931655884
0.022166967391968
Attempt #3
0.015865087509155
0.022243022918701
Attempt #4
0.015905857086182
0.022934198379517
Attempt #5
0.015322208404541
0.022816181182861
Attempt #6
0.015490055084229
0.021909952163696
Attempt #7
0.015805959701538
0.021935939788818
Attempt #8
0.01572585105896
0.022881984710693
Attempt #9
0.015491008758545
0.022812128067017
Attempt #10
0.015367031097412
0.02212119102478
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php