Google Answers Logo
View Question
 
Q: Preg_replace function - For eiffel-ga ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Preg_replace function - For eiffel-ga
Category: Computers > Programming
Asked by: pulplitcom-ga
List Price: $10.00
Posted: 11 May 2004 09:16 PDT
Expires: 10 Jun 2004 09:16 PDT
Question ID: 344661
What I'd like is a function that uses preg_replace to do the following:

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.

That is, I'd like an easy way to apply the preg_replace to any number of
<ASIN></ASIN> statements so that instead the </ASIN> will be replaced
by </a> in any number of instances.

Regards,

pulplitcom
Answer  
Subject: Re: Preg_replace function - For eiffel-ga
Answered By: eiffel-ga on 11 May 2004 14:14 PDT
Rated:5 out of 5 stars
 
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

Clarification of Answer by eiffel-ga on 11 May 2004 14:17 PDT
Hi pulplitcom,

Please disregard the text of the clarification request which I
accidentally copied from your previous question. The answer to this
question ends at the words "Regards, eiffel-ga". Sorry for any
confusion!

Request for Answer Clarification by pulplitcom-ga on 12 May 2004 13:39 PDT
Okay, there's a problem. Unless a \n sepearates the two instance of
<ASIN>, the program returns a comflated example.

So, for instance, <ASIN="1">Test 1</ASIN> <ASIN="2">Test 2</ASIN> returns

<a href="http://www.amazon.com/exec/obidos/ASIN/1/pulplit-20">Test1</ASIN>
<ASIN="2">Test2</a>

Is there an easy way to fix this problem?

Clarification of Answer by eiffel-ga on 12 May 2004 23:52 PDT
Hi pulplitcom,

If more than one ASIN code can appear on one line, then we need to
change the search from "greedy" to "non-greedy". This is done by
adding a question mark to the part of the regular expression that's
"gobbling up" those extra characters.

Here is the modified search string (the first argument to
preg_replace). Notice that ".*" has become ".*?"

!<ASIN="([0-9X]*)">(.*?)</ASIN>!

This should work no matter how many ASIN codes you have per line.

Regards,
eiffel-ga

Request for Answer Clarification by pulplitcom-ga on 13 May 2004 16:32 PDT
Good stuff. Thanks.

Clarification of Answer by eiffel-ga on 13 May 2004 23:50 PDT
Glad it did the job, pulplitcom!
pulplitcom-ga rated this answer:5 out of 5 stars
Worked as advertised. Good explanation.

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