Home > Archive > PHP Programming > April 2004 > Re: Need help replacing a specific <a href ... </a>
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Need help replacing a specific <a href ... </a>
|
|
| Pedro Graca 2004-04-28, 7:49 pm |
| 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.jpg"></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('display_errors', '1');
$x = '... <a href="http://www.sitexyz.com/page2">';
$x .= '<img height="120" src="origpic.jpg"></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 :
| |
|
| "Pedro Graca" <hexkid@hotpop.com> wrote in message
news:c6pbtn$el89j$2@ID-203069.news.uni-berlin.de...
> Sorby wrote:
>
> You realize the parameter to file() should be between quotes, right?
>
the[color=darkred]
completely[color=darkred]
>
> What about the order they appear in?
> What about white space (including newlines)?
> What about other attributes?
>
different[color=darkred]
...[color=darkred]
>
> 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('display_errors', '1');
>
> $x = '... <a href="http://www.sitexyz.com/page2">';
> $x .= '<img height="120" src="origpic.jpg"></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
| |
| Pedro Graca 2004-04-28, 7:57 pm |
| Sorby wrote:
> "Pedro Graca" <hexkid@hotpop.com> wrote in message
> news:c6pbtn$el89j$2@ID-203069.news.uni-berlin.de...
[color=darkred]
> 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"><img 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 :
| |
| Pedro Graca 2004-04-28, 8:32 pm |
| 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 =========="><img 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($regexp, $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 :
|
|
|
|
|