473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Regular expression problem - rewriting of internal links

I've spent most of the day trying to solve this problem, and much
searching has also failed to find a solution! I wonder if anyone can
suggest a solution?

Basically I need to rewrite all *internal* links on a page to add a
query string to each. For instance,

<a href="/path/to/file.html" target="_blank">
should become
<a href="/path/to/file.html?edit" target="_blank">

and

<a href="" target="_blank">
should become
<a href="?edit" target="_blank">

but

<a href="http://example.com/path/to/file.html" target="_blank">
should stay unchanged.
I've tried things like:

$search = '(<a([^>]*) href="(?<!http://)([^"]*)"';
$html = preg_replace ("~{$regexp}~i", '\1?edit', $html);

but the presence of the [^"]* seems to make the negative look-behind
assertion fail.

Any ideas gratefully received!
Sep 5 '08 #1
1 2965
tallyce wrote:
Basically I need to rewrite all *internal* links on a page to add a
query string to each. [...]
$search = '(<a([^>]*) href="(?<!http://)([^"]*)"'; $html = preg_replace
("~{$regexp}~i", '\1?edit', $html);

but the presence of the [^"]* seems to make the negative look-behind
assertion fail.
Are you sure that you need negative look behind?

Note that your regex does not match these links:
<a title="..." href="...">
<a href='...'>

This seem to work:
preg_replace('|href=[\'"](?!http://)([^\'"]*)[\'"]|',
'href="\1?query=bla"', $page);

This matches href, quote, not starting with http://, zero or more
characters which are not a quote, ended with a quote. Where a quote is '
or ".

Sep 5 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Keith Morris | last post by:
Hi all! I'm creating a mini CMS that will store content in a MySQL database. What I am trying to do is parse the content and replace certain keywords with a link. The keywords and associated...
18
by: K_Lee | last post by:
I documented the regex internal implementation code for both Tcl and Python. As much as I like Tcl, I like Python's code much more. Tcl's Stub interface to the external commands is confusing to...
0
by: radawson218 | last post by:
Hi, I'm a complete newbie when using regular expressions, so forgive me if my delimma sounds stupid. I have an ASP.NET app that utilizes url rewriting. It's a simple bookstore that allows...
4
by: Ricardo | last post by:
how wold be a regular expression to pick links from a page ???? s...
2
by: Dot net work | last post by:
Hello. Say I have a .net textbox that uses a .net regularexpressionvalidator. If the regular expression fails, is it possible to launch a small client side javascript function to do something,...
14
by: olekristianvillabo | last post by:
I have a regular expression that is approximately 100k bytes. (It is basically a list of all known norwegian postal numbers and the corresponding place with | in between. I know this is not the...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
1
by: roberto321 | last post by:
Hi Guys, I was wondering if someone could help me out with the following requirements <mydocument> <div id="other"> <a href="linkother">linkother</a> </div> <div id="hello">
10
by: venugopal.sjce | last post by:
Hi Friends, I'm constructing a regular expression for validating an expression which looks as any of the following forms: 1. =4*++2 OR 2. =Sum()*6 Some of the samples I have constructed...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.