Link to home
Start Free TrialLog in
Avatar of Travis Hydzik
Travis HydzikFlag for Australia

asked on

PHP regex help preg_replace

experts,

I need some help with some regex and the php function preg_replace.

for some reason this is not working;
 
$ret = preg_replace('/((?:href|src) *= *[\'"])(?!(http|ftp))/i', 'this is a test', $ret);

Open in new window


this is my input
 
<marker icon="Marker.png" name="gcc1b3-rockin-around-the-salt-photo-five.jpg" width="2048" height="1536" thmname="gcc1b3-rockin-around-the-salt-photo-five-th.jpg" thmwidth="160" thmheight="120" date="16/03/2008 10:59:36 AM" datum="WGS 1984" alt="164.4553" altunits="feet" title="GCC1B3 Rockin' Around the Salt" html="<div id="infotitle">GCC1B3 Rockin' Around the Salt</div><div id="infoimg"><a href='gcc1b3-rockin-around-the-salt-photo-five.jpg' title='Click for a full size view' target='_blank'><img src='gcc1b3-rockin-around-the-salt-photo-five-th.jpg' width='160' border='0' height='120' /></a></div>" lat="-20.67069" lng="116.73424"/>

Open in new window


in summary I am converting relative links to absolute.

thanks
SOLUTION
Avatar of jessc7
jessc7
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
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain 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 Travis Hydzik

ASKER

I want to add the absolute path, so if it was something like href='myfile' it would be modified to href='http://mysite.com//myfile'

I am thinking it would be something similar to
$ret = preg_replace('/((?:href|src) *= *[\'"])(?!(http|ftp))/i', '$1'.$url, $ret);
where $url is the absolute url i.e. http://mysite.com/ in the example.

if they are already absolute urls then it should be skipped.
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
what about if double quotes are used instead of single?
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
original regex was correct.
sorry, missing one set of brackets

$ret = preg_replace('/((?:href|src) *= *(?:&apos;|&quot;)(?!(http|ftp)))/i', "$1".$xml_path, $ret);

Open in new window