Forum Moderators: coopster
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
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
$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
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
[edited by: jatar_k at 10:47 pm (utc) on Oct. 1, 2003]
[edit reason] fixed the weird redirect link [/edit]
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