Solved

Using regular expressions to reformat a URL in HTML

Posted on 2003-10-23
3
227 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
3 Comments
 
LVL 1

Expert Comment

by:robsmart
Comment Utility
$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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now