PHP regex help preg_replace


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.

LVL 11
Who is Participating?
thydzikAuthor Commented:
okay, sorry to waste everyones time, but turns out my input was being converted to html entities.
so needed to look like;
$ret = preg_replace('/(?:href|src) *= *(?:&apos;|&quot;)(?!(http|ftp))/i', "$1".$xml_path, $ret);

Open in new window

will awards points accordingly
This isn't a direct answer to your question, but I wanted to offer up the free Regex Coach utility. It lets you visually see what you are matching with your regelar expressions.
Marco GasiFreelancerCommented:
Using this


you find these:

But can you say exactly what do you want as output?
You want modify these lines
<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"/>

How do you wnat modify them? I'll come back later

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

thydzikAuthor Commented:
I want to add the absolute path, so if it was something like href='myfile' it would be modified to href=''

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. in the example.

if they are already absolute urls then it should be skipped.
Marco GasiFreelancerCommented:
I would do something like:

preg_match_all("/(?<=href=')[^']*|(?<=src=')[^']*/", $ret, $matches);

foreach ($matches[0] as $value){
  $ret = preg_replace("/$value/", $url . $value, $ret);

Open in new window

thydzikAuthor Commented:
what about if double quotes are used instead of single?
thydzikAuthor Commented:
original regex was correct.
thydzikAuthor Commented:
sorry, missing one set of brackets

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

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.