Solved

Using regular expressions to reformat a URL in HTML

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

785 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