Forum Moderators: coopster

Message Too Old, No Replies

Simple Preg Match Question

         

havoc

2:12 am on Oct 1, 2003 (gmt 0)

10+ Year Member



I can't get preg match to work (yes i know i'm lame)

basically i want to search a url for a proxy cache address and if its found change it.

lets say i have

an address "cache-rl01.proxy.widgetz.com"

i wanna preg match "proxy.widgetz"
if thats found i just wanna change it to "proxy.widgetz.com"

now i need a lesson on pregmatch and i cant work it out from php.net

if (preg_match('dunno what to put here',$usrIP))
{
$usrIP="proxy.widgetz.com";
}

thanks guyz & galz :D

MonkeeSage

2:30 am on Oct 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this will do the trick...

$usrIP = "cache-rl01.proxy.widgetz.com";
$usrIP = preg_replace('/(.+)?(proxy\..+)/', "$2", $usrIP);
echo $usrIP;

Spits out "proxy.widgetz.com"

Jordan

havoc

2:38 am on Oct 1, 2003 (gmt 0)

10+ Year Member



so this will work for any proxy address? what if proxy.widgetz.com visits then straight after www.farfinnoogan.com visits it should change both? would it change it to proxy.farfinoogan.com or would it bypass the url?

I will have hundreds of urls go through this , that why i needed to match certain domains and change it as its needed.

i will test this i guess hope it works :D

havoc

2:46 am on Oct 1, 2003 (gmt 0)

10+ Year Member



it worked .. :D
thanks heaps

MonkeeSage

2:51 am on Oct 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It should probably actually be like this...

$usrIP = preg_replace('/(.+)?(proxy.+)/', "$2", $usrIP);

...so it catches "proxy3.whatever" also.

The pattern...

/(.+)?(proxy.+)/

...means (if I got it right that is, lol)...

'find anything in front of "proxy" (if there is anything) and cut if off; keep everything after "proxy" '

If there is nothing in front of "proxy" (e.g., proxy.farfinoogan.com) it will just return the whole input string (same thing if there is no "proxy" in the string at all).

Jordan

MonkeeSage

2:55 am on Oct 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right on :)

Jordan

havoc

6:00 am on Oct 1, 2003 (gmt 0)

10+ Year Member



i need a tutorial on preg match :D

its all good now :D

havoc

11:18 am on Oct 1, 2003 (gmt 0)

10+ Year Member



Is there a tutorial on preg match that explain what the /'s and the +'s do?

I also would of liked to makes all google requests to be google.com instead of google.co.uk or google.com.it

i have no idea how to do these as i dont know what all the chars are

/(.+)?(proxy.+)/

would this mean

/<==start from http://
(.+)? <== no idea
(proxy.+ <= means anything that is here)/ <end after first slash?

thanks sorry for being stupid just dont know what is what

coopster

12:49 pm on Oct 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There are a couple of links in this post [webmasterworld.com] (which is a great post to read BTW) thta give really good regular expression tutorials. (preg_match [us4.php.net] is a Perl-Compatible Regular Expresssion function).

[edited by: jatar_k at 10:47 pm (utc) on Oct. 1, 2003]
[edit reason] fixed the weird redirect link [/edit]

MonkeeSage

10:24 pm on Oct 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster:

For some reason your first link just takes me to the hilited posts page. :\

havoc:

I'm not sure if the link worked for you or not, but just in case it didn't, a brief explanation...

I used the preg_replace() because it returns a string and preg_match() returns an array; plus you wanted to replace part of the string, not just find matches in it.

Syntax is:

preg_replace('pattern', "replace string", "string to replace in");

The pattern was:

/(.+)?(proxy.+)/

/.../ = regexp pattern
() = grouping parentheticals -- you can backreference the values of different groups in the replace string by using "$<number of the group>" -- we wanted the value matched by the second group, so we backreferenced "$2" as the replacement string.
. = match any character
+ = continue matching the previous part of the pattern until the next part is found (or to the end of the string to replace in)
? = match 0 or 1 instances of the previous part of the pattern

So...

preg_replace('/(.+)?(proxy.+)/', "$2", $usrIP);

...means...

Find any chars before the string "proxy" (if there are any) in the first group, and find the string "proxy" and any chars that follow it in the second. Then replace the string to replace in with the value of the second group.

If the string to replace in begins with "proxy", then the replace operation returns the whole string (because group 2 has from the start -- "proxy" -- and any chars that follow it -- the rest of the string); or if "proxy" is not found, the replace operation replaces nothing and the string to replace in is returned in whole.

Jordan

coopster

10:42 pm on Oct 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, don't know what happened there. Can't edit it either. I'll see if the moderator can update it for us. Here is the link:
[webmasterworld.com...]