• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

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
0
thydzik
Asked:
thydzik
  • 5
  • 2
4 Solutions
 
jessc7Commented:
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.

http://weitz.de/regex-coach/
0
 
Marco GasiFreelancerCommented:
Using this

(?<=href=')[^']*|(?<=src=')[^']*

you find these:
gcc1b3-rockin-around-the-salt-photo-five.jpg
gcc1b3-rockin-around-the-salt-photo-five-th.jpg

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

Cheers
0
 
thydzikAuthor Commented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

0
 
thydzikAuthor Commented:
what about if double quotes are used instead of single?
0
 
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
0
 
thydzikAuthor Commented:
original regex was correct.
0
 
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

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now