Solved

Using regular expressions to reformat a URL in HTML

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.

705 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