PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
string

Réponse
 
LinkBack Outils de la discussion
Vieux 07/04/2008, 14h25   #1
John Taylor-Johnston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 07/04/2008, 14h27   #2
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] string

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/

  Réponse avec citation
Vieux 07/04/2008, 14h33   #3
Thiago Pojda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RES: [PHP] string

<?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


  Réponse avec citation
Vieux 07/04/2008, 14h41   #4
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
>
>
>


  Réponse avec citation
Vieux 07/04/2008, 14h59   #5
Thiago Pojda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RES: RES: [PHP] string

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
>
>
>



  Réponse avec citation
Vieux 07/04/2008, 15h18   #6
Daniel Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] string

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!
  Réponse avec citation
Vieux 07/04/2008, 15h23   #7
John Taylor-Johnston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] string

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.
>
>


  Réponse avec citation
Vieux 07/04/2008, 15h42   #8
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] string

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

  Réponse avec citation
Vieux 07/04/2008, 15h46   #9
Daniel Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] string

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!
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 04h45.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15572 seconds with 17 queries