473,769 Members | 7,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help replacing a specific <a href ... </a>

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 ... What a horrible can of worms
regex is!! (to the uninitiated at least).

I realise that if I spend the next few weeks researching regex I'll probably
find the answer but I was wondering if anyone here would kindly help speed
up the process?

Basically I am grabbing the html from another URL, finding a specific <a
href .. </a> block and replacing the contents of that block with another
link. (n.b. the code examples below are pseudo code - I realise I need to
'escape' some characters)

Here's how I'm grabbing the source html :
$html = join ("", file (http://www.sitexyz.com/index.htm));

the link I want to replace looks something like :
<a href="http://www.sitexyz.com/page2"><img height="120"
src="origpic.jp g"></a>
Note - the only thing I know to be constant about the above link is that the
height is always 120 - the link destination and source image are completely
unpredictable.

Here's a variable holding the replacement link to my site using a different
image :
$newlink = "<a href="http://www.mysite.com" ><img height="120"
src="mypic.jpg" ></a>";

So... how do I use preg_replace() or ereg_replace() to find the <a href ..
</a> block encompassing an image of height 120 and replace it with the
contents of my $newlink variable?

*** To precis my question ... How do I replace an HTML link (where all I
know is the image height) with a link of my own? ***

TIA!

p.s. I promise I'll go and sit on the top of a mountain and not come down
until I've memorised & understood everything about regex ... once I've
sorted this problem! :o)

--
Sorby

Jul 17 '05 #1
5 2600
Sorby wrote:
Here's how I'm grabbing the source html :
$html = join ("", file (http://www.sitexyz.com/index.htm));
You realize the parameter to file() should be between quotes, right?
the link I want to replace looks something like :
<a href="http://www.sitexyz.com/page2"><img height="120"
src="origpic.jp g"></a>
Note - the only thing I know to be constant about the above link is that the
height is always 120 - the link destination and source image are completely
unpredictable.
What about the order they appear in?
What about white space (including newlines)?
What about other attributes?
Here's a variable holding the replacement link to my site using a different
image :
$newlink = "<a href="http://www.mysite.com" ><img height="120"
src="mypic.jpg" ></a>";

So... how do I use preg_replace() or ereg_replace() to find the <a href ..
</a> block encompassing an image of height 120 and replace it with the
contents of my $newlink variable?
You're better off using an HTML parser than relying on the structure of
the HTML you're fetching.

Anyway, if they don't ever change, here's a starting point:

<?php
error_reporting (E_ALL);
ini_set('displa y_errors', '1');

$x = '... <a href="http://www.sitexyz.com/page2">';
$x .= '<img height="120" src="origpic.jp g"></a> ...';

$newlink = '<a href="http://www.mysite.com" >';
$newlink .='<img height="120" src="mypic.jpg" ></a>';

$y = preg_replace('@ <a href="[^"]+"><img height="120" src="[^"]+"></a>@',
$newlink, $x);

echo "$x\n$y\n";
?>

*** To precis my question ... How do I replace an HTML link (where all I
know is the image height) with a link of my own? ***


HTML links do not always have an image associated with them!
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2
"Pedro Graca" <he****@hotpop. com> wrote in message
news:c6******** ****@ID-203069.news.uni-berlin.de...
Sorby wrote:
Here's how I'm grabbing the source html :
$html = join ("", file (http://www.sitexyz.com/index.htm));
You realize the parameter to file() should be between quotes, right?


Yes - thanks. I just typed it in that way - but in my source code it has
quotes.
the link I want to replace looks something like :
<a href="http://www.sitexyz.com/page2"><img height="120"
src="origpic.jp g"></a>
Note - the only thing I know to be constant about the above link is that the height is always 120 - the link destination and source image are completely unpredictable.


What about the order they appear in?


Here is an exact cut'n'paste of the bit I want to replace.

<a href="/1/files/small/north/page2.htm"><img height="120" hspace="0"
align="left" vspace="0" border="0" width="80" alt="original photo"
src="http://www.mysite.com/origpic.jpg" /></a>
What about white space (including newlines)?
I don't think this will be a problem. Hope not anyway. All the examples I've
seen are as above and with no line-breaks/carriage-returns.
What about other attributes?
I've included them in the new example above.
Here's a variable holding the replacement link to my site using a different image :
$newlink = "<a href="http://www.mysite.com" ><img height="120"
src="mypic.jpg" ></a>";

So... how do I use preg_replace() or ereg_replace() to find the <a href ... </a> block encompassing an image of height 120 and replace it with the
contents of my $newlink variable?


You're better off using an HTML parser than relying on the structure of
the HTML you're fetching.


Am I relying on the structure of the HTML I'm fetching? If the structure of
the HTML changes over time but the image height remains 120 then the code
should still work, right?
Anyway, if they don't ever change, here's a starting point:

<?php
error_reporting (E_ALL);
ini_set('displa y_errors', '1');

$x = '... <a href="http://www.sitexyz.com/page2">';
$x .= '<img height="120" src="origpic.jp g"></a> ...';

$newlink = '<a href="http://www.mysite.com" >';
$newlink .='<img height="120" src="mypic.jpg" ></a>';

$y = preg_replace('@ <a href="[^"]+"><img height="120" src="[^"]+"></a>@',
$newlink, $x);

echo "$x\n$y\n";
?>


Thanks for taking the time to post this solution Pedro. I will try it out
now.
*** To precis my question ... How do I replace an HTML link (where all I
know is the image height) with a link of my own? ***


HTML links do not always have an image associated with them!


Good point but thankfully I am assured that in my case the links I'm looking
for will always have an image.

Thanks again - your help is greatly appreciated.

--
Sorby
Jul 17 '05 #3
"Pedro Graca" <he****@hotpop. com> wrote in message
news:c6******** ****@ID-203069.news.uni-berlin.de...
Sorby wrote:
Here's how I'm grabbing the source html :
$html = join ("", file (http://www.sitexyz.com/index.htm));


You realize the parameter to file() should be between quotes, right?
the link I want to replace looks something like :
<a href="http://www.sitexyz.com/page2"><img height="120"
src="origpic.jp g"></a>
Note - the only thing I know to be constant about the above link is that the height is always 120 - the link destination and source image are completely unpredictable.


What about the order they appear in?
What about white space (including newlines)?
What about other attributes?
Here's a variable holding the replacement link to my site using a different image :
$newlink = "<a href="http://www.mysite.com" ><img height="120"
src="mypic.jpg" ></a>";

So... how do I use preg_replace() or ereg_replace() to find the <a href ... </a> block encompassing an image of height 120 and replace it with the
contents of my $newlink variable?


You're better off using an HTML parser than relying on the structure of
the HTML you're fetching.

Anyway, if they don't ever change, here's a starting point:

<?php
error_reporting (E_ALL);
ini_set('displa y_errors', '1');

$x = '... <a href="http://www.sitexyz.com/page2">';
$x .= '<img height="120" src="origpic.jp g"></a> ...';


Sorry - I probably wasn't clear enough - the href link and the image source
name could be anything - I can't predict them - or even part of them.

--
Sorby
Jul 17 '05 #4
Sorby wrote:
"Pedro Graca" <he****@hotpop. com> wrote in message
news:c6******** ****@ID-203069.news.uni-berlin.de...
$x = '... <a href="http://www.sitexyz.com/page2">';
$x .= '<img height="120" src="origpic.jp g"></a> ...';

Sorry - I probably wasn't clear enough - the href link and the image source
name could be anything - I can't predict them - or even part of them.


It's ok, just try it with with different $x's :)
$x = '<a href="one"><img src="one"/></a>';
# $y = preg_replace();

$x = '<a href="two"><img src="two"></a>';
# $y = preg_replace();

$x = '<a href="three"><i mg src="three"></a>';
# $y = preg_replace();

....

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #5
Sorby wrote:
Here is an exact cut'n'paste of the bit I want to replace.

<a href="/1/files/small/north/page2.htm"><img height="120" hspace="0"
align="left" vspace="0" border="0" width="80" alt="original photo"
src="http://www.mysite.com/origpic.jpg" /></a>


Hmmmm ... and you want to change just the URL and image SRC ?

<a href="========= == CHANGED =========="><im g height="120" hspace="0"
align="left" vspace="0" border="0" width="80" alt="original photo"
src="========== ==== CHANGED ==========" /></a>

Copy the stuff that you want to keep to the regexp and for the stuff you
want replaced use
[^"]+
which means: one or more of anything except quotes

so

$regexp = '@' .
'<a href="[^"]+"><img height="120" hspace="0" ' .
## =URL=
'align="left" vspace="0" border="0" width="80" alt="original photo" ' .
'src="[^"]+" /></a>' .
## =SRC=
'@';

$destin = preg_replace($r egexp, $newlink, $source);
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #6

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

Similar topics

2
8777
by: Charles Nadeau | last post by:
Hello, I am trying to craft a regular expression to filter an URL from a <a href=""></a> tag and the one I have doesn't seen right. I use the regular expression from this snippet of code: foreach my $message (@messages) { my @match=($message->decoded=~/\bhref="(http.*)">.*/gi);
4
62111
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I supposed to write this function? String.replace(/</g,'&lt;');
2
2146
by: Erik Ronne | last post by:
Hello Gurus I have an intranet web page with links to all kinds of Microsoft Word document that we use at my work, so my co-works can go to the web page when they need a special document. However, when they click the links the documents are of cause loaded in the web browser MS Explorer and not opened in MS Word, which is annoying.
1
1903
by: mdbrown | last post by:
Hi guys, I have been searching for solutions, but in vain. Hope i'll be able to source for answers here from you guys, thanks in anticipation. I have this application that works fine in IE7, but not in mozilla. I have this left menu bar that is suppose to have a column with 2 sub menu. In IE7, the sub menu is able to be shown, but sad to say, in mozilla, it doesn't budge. My codes snippet being, <li class="top" id="clothes"><a...
1
1543
by: Rob | last post by:
I have values like <span id = "URL">URL</span> where the id "URL" is generated by javascript automatically. I know in JSP, one can pass the value directly like <a href = <%=URL>> but how do I do this for javascript?
2
2015
by: Peter Laman | last post by:
In my app I need to dynamically generate a series hyperlinks. Each hyperlink's action must be to focus a field in a <form>. I created the following function to create such a link (the argument is a field object, e.g. <input>): function createFieldAnchor(field, linkText) { var anchor = document.createElement("a"); anchor.field = field; anchor.href='javascript:this.focusField()';
1
4965
by: ghjk | last post by:
In my php page there is a search function and when user search it will display table containing search results. I want to add <a href ..></a> . Because when user click one record I want to pass values to ajax page. to those results. But my code is not working. Please help me.This is my code. while($row = mysql_fetch_array($result)) { echo "<a href='javascript:void(0);'...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10048
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.