asdeww22
asked on
Perl string replace for refred url
I use below code to replace string in my .cgi script
Examples: see this demo
$start=~ s/$STR_1/$STR_2/g ;
This code replace STR_1 via STR_2 but if STR_1 was contain of a URL in page do not work. exactly if we have page that contain url contain STR_1 , do not replace.Examples: see this demo
https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/
username "free", password "speech" , for example if i want replace support to sos ,Tech Support in menu change to Tech sos but refer link for Tech Support this link
https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/support/
do not change to
https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/sos/
how can do replace for link?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
my URL sometime start by http
i test
but do not replace STR_1 in URL
how can replace STR_1 everywhere in page source ?
my URL sometime start by http
i test
$start =~ s/$STR_1/$STR_2/g unless ($start =~ m{^http?://});
but do not replace STR_1 in URL
how can replace STR_1 everywhere in page source ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As ozo said, it should be m{^https?://} in order to match both http and https (as I had in my original post). If there can be other protocols, those can be added to the regex or Regexp::Common May have a full URL regex (don't remember for sure).
When you test
$start =~ s/$STR_1/$STR_2/g unless ($start =~ m{^http?://});
What are the values of $start, $STR_1, and $STR_2?
I would suggest that the points be split between:
ozo (https:#a41416041)
wilcoxon (https:#a41413321)
Ozo's later comments are asking for clarification.
The original poster never tested the correct version of wilcoxon's solution (https? rather than http?) after it was pointed out to him.
ozo (https:#a41416041)
wilcoxon (https:#a41413321)
Ozo's later comments are asking for clarification.
The original poster never tested the correct version of wilcoxon's solution (https? rather than http?) after it was pointed out to him.
Open in new window
printshttps://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/sos/
Open in new window
also printshttps://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/sos/