Hi pulplitcom,
I started with the call to 'ereg_replace' from your previous question:
Google Answers: ASIN Replace Text
http://answers.google.com/answers/threadview?id=334999
Then I changed the call to 'preg_replace', so that it would replace
the matched substring every time it appears in your text (and not just
the first time).
It turned out that some changes were also needed to the regular expression.
Firstly, the pattern passed to 'preg_replace' must be enclosed in a
delimiter, which should be some character that does not appear in the
pattern. The slash is frequently used, but in this case we have a
slash in the pattern so it is not suitable. I chose to use '!'
instead.
Next, I changed the back-references ("\\1" and "\\2") to the form that
has been preferred since PHP 4.0.4 for preg_replace: they became
"${1}" and "${2}".
Assuming that your original text is in the variable $a, you can use
the following function call to convert any number of <ASIN> tags to
the hyperlinks that you need:
function asin_replace($a) {
return preg_replace('!<ASIN="([0-9X]*)">(.*)</ASIN>!',
'<a href="http://www.amazon.com/exec/obidos/ASIN/${1}/pulplit-20">${2}</a>',
$a);
}
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 preg_replace('!<ASIN="([0-9X]*)">(.*)</ASIN>!',
'<a href="http://www.amazon.com/exec/obidos/ASIN/${1}/pulplit-20">${2}</a>',
$a);
}
echo asin_replace('<ASIN="2844140858">Test1</ASIN> Some other text.'
. "\n" . '<ASIN="2844140111">Test2</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.4
<a href="http://www.amazon.com/exec/obidos/ASIN/2844140858/pulplit-20">Test1</a>
Some other text.
<a href="http://www.amazon.com/exec/obidos/ASIN/2844140111/pulplit-20">Test2</a>
I trust that this meets your requriements. Please request
clarification if there is any problem.
Additional references:
PHP: preg_replace - Manual
http://uk.php.net/preg_replace
PHP: preg_match - Manual
http://uk.php.net/manual/en/function.preg-match.php
Google Search Strategy:
php preg_replace
://www.google.com/search?q=php+preg_replace
php "unknown modifier" preg_match
://www.google.com/search?q=php+%22unknown+modifier%22+preg_match
Regards,
eiffel-ga
Request for Answer Clarification by pulplitcom-ga on 24 Apr 2004 16:26 PDT
This is outside the scope of the original question, but if possible
I'd like to ask a follow up question.
If $a = "<ASIN="2844140858">Test1</ASIN> Some other text.
<ASIN="2844140858">Test2</ASIN>
Ending text.",
the ereg_replace on the whole text outputs
<a href="http://www.amazon.com/exec/obidos/ASIN/2844140858/pulplit-20">Test1</ASIN>
Some other text.<ASIN="2844140858">Test2</a>Ending text.
Is there an easy way to apply the ereg_replace any number of
<ASIN></ASIN> statements so that instead thte </ASIN> will be replaced
by </a> in any number of instances?
Regards,
pulplitcom |