Re: New to php3 - pattern matching?

From: Date: Sat, 22 Jul 2000 17:22:39 +0000
Subject: Re: New to php3 - pattern matching?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-7702@lists.php.net to get a copy of this message
> At 03:32 AM 7/21/00 -0400, Sterling Hughes wrote: > >> have just installed php3 and mysql etc ... have used perl before > >> > >> if I have $item which has a "/" in it and I want to remove it - perl > >> would be something like $item =~ s/\///; > > > >$item = preg_replace("/\//", "", $item); > > > >-Sterling > > Now I would have said: > > str_replace ("/", "", "$item"); > > Can anyone explain the benefit of using preg_replace over str_replace? The > examples I see on this list always look so dang obfuscated :) > For that example str_replace() is fine, however, I don't thing Geoff is asking about that example in particular, but rather regular expression matching in PHP in general... The PCRE functions offer the power of Perl's regular expressions and are therefore suitable for more complex tasks (for example removing everything third word in a sentence) whereas for simple tasks, such as the one above str_replace() or an equivalent function is simpler and faster. -Sterling

« previous php.general (#7702) next »