?
Solved

Using regular expressions to reformat a URL in HTML

Posted on 2003-10-23
3
Medium Priority
?
233 Views
Last Modified: 2012-06-27
Hello!

I'm having a hard time with regular expressions.
Basically, my goal is to format something like:

<img src="/images/pic.jpg" border=0>
<img border=0 src="images/pic.gif">

into

<img src="http://www.server.com/images/pic.jpg" border=0>
<img border=0 src="http://www.server.com/images/pic.gif">

Being new to regular expressions, this is as far as I got.
There is one catch, this expression MUST ignore entries with the server already specified.. ie: http://www.server.com/images/pic.gif


eregi_replace("src=\"(((\.\.)*/)|(^images))((([A-Za-z0-9.\-_])*)([A-Za-z0-9./-_])*)","src=\"".$url."\\5",$body);

I'm hoping someone out there can give me a hand!

Thanks a lot!
0
Comment
Question by:bot_alpha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Expert Comment

by:robsmart
ID: 9614603
$url = 'http://robsmart.com/';

$html = '<br><img border=0 src="images/pic.gif"><br>';

$html = eregi_replace('(.*src=\")\/*(images.*)','\\1'.$url.'\\2',$html);


this should do what you want.

0
 
LVL 11

Accepted Solution

by:
shmert earned 500 total points
ID: 9615241
Try this:
<?php
$html = <<<EOT
        <img src="/images/pic.jpg" border=0>
        <img border=0 src="images/pic.gif">
        <img border=0 src="http://absolute_url.com/images/pic.gif">
EOT;

$base_href = 'http://www.server.com';
$pattern = '|src="(/?)([^(http)][^"]*)"|i';
$html = preg_replace($pattern, "src=\"$base_href/\\2\"", $html);

echo '<pre>';
echo htmlspecialchars($html);
echo '</pre>';
?>

Note:  you should be able to achieve the same end result by using <base href="http://www.server.com"> in the HTML <head> section.
0
 

Author Comment

by:bot_alpha
ID: 9615767
Thanks for your quick replies!

I couldn't use <base href="http://www.server.com"> because of the way this particular application was designed.

Great job!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question