Re: Parsing brackets in text
| From: | Dotan Cohen | Date: | Mon, 13 Nov 2006 13:02:53 +0000 |
| Subject: | Re: Parsing brackets in text | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-244547@lists.php.net to get a copy of this message | ||
On 13/11/06, Roman Neuhauser <neuhauser@sigpipe.cz> wrote:
# dotancohen@gmail.com / 2006-11-13 01:35:44 +0200: On 13/11/06, Chris <dmagick@gmail.com> wrote:In the first example on that page, it looks to me like it should just return the text "next_year", and not call that function. Obviously, it is my misunderstanding of the way that the funciton works, and not in the function itself. I do learn php from TFM and this list, not from a course. Just like in any learning environment, I could misinterpret what is taught to me and need to ask a question, or not even know that I misinterpreted and not know that I need to ask a question. This is a case of the latter, clearly, because I have never needed to do anything other than a simple str_replace until now.<?php function makeLink($matches) {I didn't know that I had to call the function like that- I don't remember ever seeing it called that way.$returnString="<b>" . $matches[1] . "</b>"; return $returnString;} $articleText="This is a very [long] string."; $articleText=preg_replace_callback('/\[([a-z]+)\]/i' , "makeLink", $articleText); print $articleText . "\n"; ?> The callback takes whatever the regular expressions returns (alpha characters between [ and ]) and runs it through function "makeLink". Not sure how that will go with multiple []'s etc in the same string but it should get you started.It's done this way in all the examples on http://cz.php.net/manual/en/function.preg-replace-callback.php What documentation did you read?
Also, I must google the meaning of the (~+) in the regex.That I have read at least three or four times. I still don't capture all of it, but I try. You are correct, I should have referenced that right away. It won't hurt for me to read it again, too. Thank you. Dotan CohenNo need to google:http://cz.php.net/manual/en/reference.pcre.pattern.syntax.phpSkip the Differences From Perl section.