|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
$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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
John Taylor-Johnston 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 Either http://php.net/strpos or http://php.net/stripos if your version of PHP supports it. -Stut -- http://stut.net/ |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
<?php
$name = "John Taylor"; if (strpos($name,'John') > 0){ //you could use stripos for case insensitive search echo "found"; } ?> -----Mensagem original----- De: John Taylor-Johnston [mailto:jt.johnston@USherbrooke.ca] Enviada em: segunda-feira, 7 de abril de 2008 10:25 Para: PHP-General Assunto: [php] string $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 General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Thiago Pojda wrote:
> <?php > $name = "John Taylor"; > if (strpos($name,'John') > 0){ > //you could use stripos for case insensitive search > echo "found"; > } > ?> This will not do what you expect it to. Since 'John' is the first thing in the string strpos will return 0 causing the condition to evaluate to false. As per the documentation for strpos you should compare the value *and type* of the variable returned by strpos against false to check for non-existance. if (strpos($name, 'John') !== false) { ... } -Stut -- http://stut.net/ > -----Mensagem original----- > De: John Taylor-Johnston [mailto:jt.johnston@USherbrooke.ca] > Enviada em: segunda-feira, 7 de abril de 2008 10:25 > Para: PHP-General > Assunto: [php] string > > $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 General Mailing List (http://www.php.net/) To unsubscribe, > visit: http://www.php.net/unsub.php > > > |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Never late to learn new stuff, you're right Stut.
Thanks! -----Mensagem original----- De: Stut [mailto:stuttle@gmail.com] Enviada em: segunda-feira, 7 de abril de 2008 10:42 Para: Thiago Pojda Cc: 'John Taylor-Johnston'; 'PHP-General' Assunto: Re: RES: [php] string Thiago Pojda wrote: > <?php > $name = "John Taylor"; > if (strpos($name,'John') > 0){ > //you could use stripos for case insensitive search > echo "found"; > } > ?> This will not do what you expect it to. Since 'John' is the first thing in the string strpos will return 0 causing the condition to evaluate to false. As per the documentation for strpos you should compare the value *and type* of the variable returned by strpos against false to check for non-existance. if (strpos($name, 'John') !== false) { ... } -Stut -- http://stut.net/ > -----Mensagem original----- > De: John Taylor-Johnston [mailto:jt.johnston@USherbrooke.ca] > Enviada em: segunda-feira, 7 de abril de 2008 10:25 > Para: PHP-General > Assunto: [php] string > > $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 General Mailing List (http://www.php.net/) To unsubscribe, > visit: http://www.php.net/unsub.php > > > |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Mon, Apr 7, 2008 at 9:25 AM, John Taylor-Johnston
<jt.johnston@usherbrooke.ca> 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. -- </Daniel P. Brown> Ask me about: Dedicated servers starting @ $59.99/mo., VPS starting @ $19.99/mo., and shared hosting starting @ $2.50/mo. Unmanaged, managed, and fully-managed! |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Excellent. Thanks all!
John Daniel Brown wrote: > On Mon, Apr 7, 2008 at 9:25 AM, John Taylor-Johnston > <jt.johnston@usherbrooke.ca> 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. > > |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Daniel Brown wrote:
> On Mon, Apr 7, 2008 at 9:25 AM, John Taylor-Johnston > <jt.johnston@usherbrooke.ca> 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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On Mon, Apr 7, 2008 at 10:42 AM, Jim Lucas <lists@cmsws.com> wrote:
> > I wonder if using strstr() with strtolower() would be faster or slower. [snip=code] > > Results: > Attempt #1 > 0.015728950500488 > 0.022881031036377 [snip!] While I don't really care much for a single selection about the difference of less than 7/1,000's of one second, that's still good information to keep in mind - especially when dealing with large-scale sites. Nice work, Jim. -- </Daniel P. Brown> Ask me about: Dedicated servers starting @ $59.99/mo., VPS starting @ $19.99/mo., and shared hosting starting @ $2.50/mo. Unmanaged, managed, and fully-managed! |
|
![]() |
| Outils de la discussion | |
|
|