Hi pulplitcom,
Assuming that your original text is in the variable $a, you can use
the following function call to convert the <ASIN> tag to the hyperlink
that you need:
ereg_replace('<ASIN="([0-9X]*)">(.*)</ASIN>',
'<a href="http://www.amazon.com/exec/obidos/ASIN/\\1/refferer_ID">\\2</a>',
$a);
In the replacement string (the second string passed to ereg_replace)
the escape code \\1 represents the book number and \\2 represents the
book title (because we have enclosed those parts in parentheses in the
search string regular expression).
Here's a small PHP program that can be run from the command line to
illustrate how this all works. I checked it using PHP 4.3.3 under
Fedora Core 1 Linux:
<?php
function asin_replace($a) {
return ereg_replace('<ASIN="([0-9X]*)">(.*)</ASIN>',
'<a href="http://www.amazon.com/exec/obidos/ASIN/\\1/refferer_ID">\\2</a>',
$a);
}
echo asin_replace('<ASIN="2844140858">Book Title</ASIN>') . "\n";
?>
Here is the output of this program when run from the command line:
Content-type: text/html
X-Powered-By: PHP/4.3.3
<a href="http://www.amazon.com/exec/obidos/ASIN/2844140858/refferer_ID">Book
Title</a>
I trust that this meets your requriements. Please request
clarification if there is any problem.
Additional references:
PHP: ereg_replace - Manual
http://uk.php.net/ereg_replace
Google Search Strategy:
php ereg_replace
://www.google.com/search?q=php+ereg_replace
php "define a function"
://www.google.com/search?q=php+%22define+a+function%22
Regards,
eiffel-ga |
Clarification of Answer by
eiffel-ga
on
25 Apr 2004 02:56 PDT
Hi pulplitcom,
If you want to replace all the occurrences of <ASIN>...</ASIN> at
once, you can use the preg_replace function.
However, preg-replace uses PERL-syntax regular expressions rather than
POSIX-syntax regular expressions as used by ereg_replace.
That's probably enough information for you to get it to work. However,
if you would prefer me to do that for you, you could post a new
question with "For eiffel-ga" in the subject line, and I can test it
out to see what (if any) differences will be necessary in the regular
expression. I don't know the answer off the top of my head, as I
normally use the POSIX forms.
Regards,
eiffel-ga
|