On 13/11/06, Chris <dmagick@xxxxxxxxx> wrote:
Dotan Cohen wrote: > On 12/11/06, Chris <dmagick@xxxxxxxxx> wrote: >> Dotan Cohen wrote: >> > I need to replace text enclosed in brackets with links. How can I get >> > that text, though? I've tried numerous variations of the following, >> > with no success: >> > $articleText=preg_replace('/\[[a-z]]/i' , makeLink($1), $articleText); >> > >> > I cannot get the text between the brackets send to the makeLink >> > function. Could someone push me in the right direction? An example of >> > the text would be: >> > $articleText="Ajax is an acronym of Asynchronous [JavaScript] and >> [XML]"; >> > >> > Where I'd like the strings "JavaScript" and "XML" sent to the makeLink >> > function. Thanks in advance. >> >> Don't know if this is your problem but you need to escape the trailing ] >> as well: >> >> preg_replace('/\[[a-z]\]/i', ..... >> > > Thanks, but that wasn't quite it, either. This code: > > <?php > function makeLink($title) { > $returnString="<b>$title</b>"; > return $returnString; > } > > $articleText="This is a very [long] string."; > $articleText=preg_replace('/\[[a-z]\]/i' , makeLink($1), $articleText); > print $articleText; > ?> > > produces this error: > Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE > or '$' in /home/user/public_html/testpage.php on line 9 > > I'm sorry that I didn't include the error message in my original > email. Also, I got the same error with and without the last closing > bracket escaped. Ah, completely missed that part. You can't pass "$1" through like that. You have to do some funky quoting to do what you want in one go. http://php.net/preg_replace Check "Example 4. Using the 'e' modifier" or use a callback: http://www.php.net/manual/en/function.preg-replace-callback.php
I had looked at both of those already- with neither luck nor understanding. I can fuddle my way through a simple regex, but that monster has me bewildered. I'm pretty sure that preg_replace is all I need, but I can't figure out the correct syntax for referencing a chunk of the data. I'm of course not asking anybody to write the regex for me, rather to show me how it must be so that I'll know how to construct them. I saw another, similar example on ilovejackdaniels.com where the author had parsed Google query strings, but that example was even more confounded than the one in TFphpM. Dotan Cohen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php