Solved

Using regular expressions to reformat a URL in HTML

Posted on 2003-10-23
3
230 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 125 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link failure 16 63
Ajax and PHP 4 47
Looking for suggestions on different user roles best design practices 4 28
PHP Curl Problem 10 36
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

756 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