473,406 Members | 2,633 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,406 software developers and data experts.

Replacing part of link with regexp

Hello!

Example:
<a href="www.abc.com">ABC</a>
<a href="www.xyz.com">www.xyz.com</a>

I'm looking for a script that replaces the part within the HTML tag
(www.abc.com and www.xyz.com) with another link (in my case a
redirect), without affecting the part with "www.xyz.com: enclosed by
the tags.

I've tried something myself which replaces the url's, but all strings
that resemble an url

function replaceUrls($text)
{
global $BASE_URL;

$pattern = "[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]";
$replacement = "\\0";
$text = eregi_replace($pattern, $replacement, $text);

$endoftext = 0;

while (!$endoftext)
{
$start = strpos($text, "");
$end = strpos($text, "
");

if (($start) && ($end))
{
$url = substr($text, $start + 5, $end - $start - 5);

$newurl = "redirect.php?url=$url";
$oldurl = "" . $url . "";

$text = str_replace($oldurl, $newurl, $text);
}
else
{
$endoftext = 1;
}
}
return $text;
}

Aug 26 '05 #1
1 1748
Hello!
<a href="www.abc.com">ABC</a>
<a href="www.xyz.com">www.xyz.com</a>

I'm looking for a script that replaces the part within the HTML tag
(www.abc.com and www.xyz.com) with another link (in my case a
redirect), without affecting the part with "www.xyz.com: enclosed by
the tags.


This should work:

<?php

$link = '<a href="http://www.google.com" target="_bla">foobar</a>';
$my_url = 'http://www.example.com/redirect.php?url=$1';

echo preg_replace('/(?<=\<a href\=\")([^\"]+)(?=\")/', $my_url, $link);

?>

Of course $link could be any text containing links.

Greetings,
Hero Wanders
Aug 26 '05 #2

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

Similar topics

5
by: Sorby | last post by:
Hi I've been coding in PHP for a little while now and was starting to feel pretty confident but then realise I need to understand regular expressions to solve a particular problem I've got ......
3
by: DrewM | last post by:
I'm sure this isn't difficult, but it's Friday afternoon (!). I'm trying to use a regular expression to match html tags in a string and convert them to lower case. It's the RegExp object that I'm...
6
by: Oliver Spiesshofer | last post by:
Hi, I would like to replace all strings in a table with regexp: the strings contain the substring "-na", and I would like to replace the whole table field with the original content but without...
8
by: timmy_dale12 | last post by:
I need help with this one. I have a function that pastes a row. In each row i am pasting a link which is to call a date picker javascript function. This is the code that pastes the link : link =...
0
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
14
by: Adnan Siddiqi | last post by:
Hi Suppose I have following URLs comming from an HTML document <a href="http://mydomain1.com">Domain1</a> <a...
2
by: Brad | last post by:
I have the following function: function updaterec(i, callid){ var theExp = new RegExp(callid, 'gi'); var x = document.getElementById(i.id.replace(RegExp,'tb_linechage')); x.value = 'Y';} I...
4
by: Andrew Poulos | last post by:
I have a string that looks like this "cmi.interactions.fred.id" I need to test that: - the first part of the string is "cmi.interactions." (case sensitive). - the last part of the string is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.