Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 2nd, 2004, 12:25 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default Need RegEx help

Hello,

I need a pattern that parses some HTML code and replaces all src and href values to include the absolute path to the resource.

So <a href='/index.php'> turns into <a href='http://www.mysite.com/index.php'> and <link href="styles.css"/> turns into <link href="http://www.mysite.com/directory/styles.css"/>

I would do it myself but I'm very new to regular expressions and can't figure it out.

Thanks,

-Snib <><
http://www.snibworks.com
There are only two stupid questions: the one you don't ask, and the one you ask more than once ;)
__________________
-Snib - http://www.snibworks.com
Where will you be in 100 years?
 
Old October 4th, 2004, 03:21 AM
Registered User
 
Join Date: Sep 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Bit new to it myself, but

$picture="<a href='/index.php'>";
$picture= ereg_replace ( "<a href='/index.php'>", "<a href='http://www.mysite.com/index.php'>",$picture);
echo $picture;

Should work.


 
Old October 4th, 2004, 04:52 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Richard,

That will replace all instances of <a href='/index.php'> but what if it doesn't link to /index.php? What if I used double quotes (")? What if the tag really looks like this: <a style='color:red' href='/index.php'>. And also I need it to parse <img> tags, <script> tags and <link> tags.

Thanks for helping,

-Snib <><
http://www.snibworks.com
There are only two stupid questions: the one you don't ask, and the one you ask more than once ;)
 
Old October 8th, 2004, 09:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

hey Snib

i gotta admit that regular expressions are not my favorites... but this is what i came up with for your problem... (might be buggy)....

<?php

$teststr = "<a href='index.php'>";
$newstr = preg_replace("/\<(a|link) href='(.*?)'>/","<\\1 href=\"http://www.mysite.com\\2\">",$str);

echo $newstr;

?>

after this script is parsed, you will not see anything on the screen (browser's window), but look at the html code ("view source")

hope that helped u :D

crazy zoltalar


www.campusgrind.com the college portal
 
Old October 20th, 2004, 12:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There's a problem with your regex replacement, Moharo. Check your own example: "index.php" gets matched as \2, so your replacement shows up as:

"http://www.mysite.comindex.php"

There are a bunch of cases you need to test for.

1) Does the link already specify an absolute URL?
    (e.g. http://www.example.com/foo/bar/page.html)

2) Does the link specify an absolute path?
    (e.g. /foo/bar/page.html)

3) Does the link specify a path relative to the current script?
    a) at or below the current path?
        (e.g. page.html, foo/bar/page.html)
    b) above or a sibling to the current path?
        (e.g. ../page.html, ../foo/bar/page.html)


For 1), do nothing. No replacement necessary.

For 2), simply prepend the host to the path.

for 3), you'll need to calculate the working directory of the currently executing path, and
    for a) append the relative url to the working directory.
    for b) modify the working directory to reflect the ".."s in the relative path.



Make sense?


Take care,

Nik
http://www.bigaction.org/
 
Old October 20th, 2004, 03:19 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Welcome back, Nik.

You seem to know your way around regular expressions better than either of us, could you try to make a pattern for this?

I am still trying myself, unsuccessfully.

Thanks,

-Snib <><
Try new FreshView 0.2!
There are only two stupid questions: the one you don't ask, and the one you ask more than once ;)
 
Old November 16th, 2004, 06:59 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

may b good url2learn regular expressions:
http://www.webreference.com/js/column5/






Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex pendemv JSP Basics 4 December 8th, 2008 08:29 AM
regex mrame XSLT 12 July 25th, 2008 09:37 AM
Regex htran XSLT 2 May 18th, 2005 09:21 AM
More regex help Snib Pro PHP 4 December 16th, 2004 09:56 PM
Regex Help boyer99g General .NET 2 October 8th, 2004 05:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.