Link to home
Start Free TrialLog in
Avatar of asdeww22
asdeww22

asked on

Perl string replace for refred url

I use below code to replace string in my .cgi script

$start=~ s/$STR_1/$STR_2/g ;

Open in new window

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.co‌​m/

Open in new window

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/

Open in new window

do not change to
https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.co‌​m/html/sos/

Open in new window

how can do replace for link?
Avatar of ozo
ozo
Flag of United States of America image

$start='https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/support/';
$STR_1='/support/';
$STR_2='/sos/';
$start=~ s/$STR_1/$STR_2/g ;
print $start;

Open in new window

prints
https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/sos/

$start='https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/support/';
$STR_1='https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/support/';
$STR_2='https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/sos/';
$start=~ s/$STR_1/$STR_2/g ;
print $start;

Open in new window

also prints
https://www.prismnet.com/~jsm/cgiproxy/nph-demo.cgi/en/20/https/www.prismnet.com/html/sos/
SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asdeww22
asdeww22

ASKER

Hi
my URL sometime start by http

i test
$start =~ s/$STR_1/$STR_2/g unless ($start =~ m{^http?://});

Open in new window


but do not replace STR_1 in URL
how can replace STR_1 everywhere in page source ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?://});

Open in new window

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.