Google Answers Logo
View Question
 
Q: ASIN replace text ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: ASIN replace text
Category: Computers > Programming
Asked by: pulplitcom-ga
List Price: $10.00
Posted: 23 Apr 2004 11:29 PDT
Expires: 23 May 2004 11:29 PDT
Question ID: 334999
Hi-

What I'd like is a ereg_replace example written in php, so that if
given the text <ASIN="2844140858">Book Title</ASIN>

the ereg_replace will return <a
href="http://www.amazon.com/exec/obidos/ASIN/2844140858/refferer_ID">Book
Title</a>.

Thanks,
Answer  
Subject: Re: ASIN replace text
Answered By: eiffel-ga on 24 Apr 2004 03:56 PDT
Rated:5 out of 5 stars
 
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

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

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
pulplitcom-ga rated this answer:5 out of 5 stars
Perfect. Worked like a charm. Many thanks. Also, appreciate the clear
explanation; was beating my head against a wall trying to figure out
regular expressions myself.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy