help: eregi_replace()

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
karenhuang
Forum Newbie
Posts: 4
Joined: Wed Oct 25, 2006 9:39 am

help: eregi_replace()

Post by karenhuang »

I have the following cpde to understand, however, I am not quite sure about

$loc = eregi_replace("[[:space:]]+", " ",str_replace(","," ",$info["CITY"])

$info is an String array, with one of keys "CITY".

It do some string replace, and then what?

What's "[[:space:]]" used for??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

[:classname:] are predefined sets of characters. [:space:] includes all kind of whitespaces like space, tab, line breaks and so on.

Please use pcre (pcre_replace) instead of posix regular expressions (eregi_replace).
http://de2.php.net/regex wrote:Tip: PHP also supports regular expressions using a Perl-compatible syntax using the PCRE functions. Those functions support non-greedy matching, assertions, conditional subpatterns, and a number of other features not supported by the POSIX-extended regular expression syntax.
http://de2.php.net/regex wrote:These regular expression functions are not binary-safe. The PCRE functions are.
karenhuang
Forum Newbie
Posts: 4
Joined: Wed Oct 25, 2006 9:39 am

Post by karenhuang »

Thanks a lot. That's really clear for me now.
karenhuang
Forum Newbie
Posts: 4
Joined: Wed Oct 25, 2006 9:39 am

Post by karenhuang »

Still have some questions about this??

$loc = eregi_replace("[[:space:]]+", " ",str_replace(","," ",$info["CITY"])

Then why it uses [[: :]] instead of [::] -- "[[:space:]]" , and what's "+" for after "[[:space:]]" ??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

[ ] marks a set of characters, like [0-9].
You can use predefined classes within other sets, like [[:space:]a-k]
But here it's just redundant, could be [:space:] as well.
+ means "1 or more occurrences"
* means "0 or more occurrences"
? means "0 or 1 occurrence"
karenhuang
Forum Newbie
Posts: 4
Joined: Wed Oct 25, 2006 9:39 am

Post by karenhuang »

Now it's really clear now. Thank volka.
Post Reply