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 > RE: [PHP] string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
RE: [PHP] string

Réponse
 
LinkBack Outils de la discussion
Vieux 07/04/2008, 14h30   #1
admin@buskirkgraphics.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] string

Do a preg match to find one or preg_match_all to find all the john in the string.

<?php
$name = "John Taylor";
$pattern = '/^John/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>





$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, 15h36   #2
Mark J. Reed
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] string

On Mon, Apr 7, 2008 at 9:30 AM, <admin@buskirkgraphics.com> wrote:
> Do a preg match to find one or preg_match_all to find all the john in the string.


preg_* is overkill if you're just searching for a literal string. use
it if you're searching for any strings matching a pattern, part of
which you don't know. If you know the entire string you're looking
for, strstr() is both more efficient and easier to use.

Now, strpos() is more efficient still, but arguably more annoying to
use because of the "0 but true" issue that necessitates checking for
!== false.

Besides efficiency, the only difference between strstr() and strpos()
is what they return. strpos() returns the index of the first match,
while strstr() returns the entire string starting from that point;
it's the building of the copy of the string that causes strstr() to be
less efficient.

Both functions have case-insensitive variants stristr and stripos.

In each case, if the substring occurs more than once within the outer
string, the return value is based on the *first* occurrence. strpos()
(but not strstr()) has a variant that uses the *last* one instead:
strrpos() (r=reverse), which also has a case-insensitive version
strripos(). You can easily define a strrstr() though:

function strrstr($where, $what) {
$pos = strrpos($where, $what);
return $pos === false ? false : substr($where, $pos);
}

And for good measure, a strristr:

function strristr($where, $what) {
$pos = strripos($where, $what);
return $pos === false ? false : substr($where, $pos);
}




>
> <?php
> $name = "John Taylor";
> $pattern = '/^John/';
> preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
> print_r($matches);
> ?>
>
>
>
>
>
>
>
> $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
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>




--
Mark J. Reed <markjreed@mail.com>
  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 23h14.


É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,09574 seconds with 10 queries