Page 1 of 1
[Solved]preg_replace confusion
Posted: Tue Mar 30, 2004 10:21 am
by Steveo31
Hey all. I'm learning the preg_*, and after going through php.net's manual about 10 times I'm having some problems with this one here:
Code: Select all
preg_match("/^(http:\/\/)?([^\/]+)/i","http://www.php.net/index.html",$matches);
I know how it is translating up to the "?[^\/]+" part. What is this part saying? What is it negating?
Also, the "?" is confusing me as it has more than one meaning. One is "to extend the meaning of", and the other being "a 0 or 1 quantifier".
Cound you clear this up for me?

Posted: Tue Mar 30, 2004 11:26 am
by twigletmac
The [^\/]+ says to look for any character except a forward slash. The ? mark indicates that there can be 0 or 1 instances of 'http://' to start the string.
Mac
Posted: Tue Mar 30, 2004 2:24 pm
by Steveo31
That makes some sense. But why the backslash in there first? I guess it hase to be escaped...
The ? stll gets me though. In this case I understand, but I don't see why it would be used instead of * or +.
Posted: Tue Mar 30, 2004 2:43 pm
by twigletmac
? would be used instead of + or * because you want to make sure that there is 0 or 1, not any number - you don't want to match something like
http://http://http://www.site.com.
The backslash escapes the forward slash because the forward slash is being used as the pattern delimiter - you could probably change the whole thing to:
Mac
Posted: Tue Mar 30, 2004 3:02 pm
by Steveo31
Perfect. Thanks Mac.
Posted: Tue Mar 30, 2004 6:12 pm
by tim
http://www.zend.com/zend/tut/tutorial-delin2.php
if you havent read that reg ex tut, tis the one I thought I advanced most with.
=]
Posted: Tue Mar 30, 2004 7:03 pm
by Steveo31
Ah nice one. Agreed
