Monday 15 August 2011

php - What defines a regex section to be replaced in preg_replace()? -


I have just started with PHP regular expression. I understand how to read and write them (I need my book because I do not remember the symbols of any pattern). I really want to use RegExp for BB code on my site using the preg_replace

I understand the standards, but I do not understand what defines can be changed in the pattern? What I've done so far:

  preg_replace ('/ (\ [url = http: \ / \ /.* \]) /', '& lt; a href = "$ 1 "& Gt; $ 2 & lt; / a & gt; '," [url = http: //google.com] ");   

Now, I know that this is probably not "intelligent" security, I just want to do some work. I match the entire string ... so I get a link which is mysite / [url = http: //google.com] . looks like.

I defined the PHP manual

  • what is replaced with the string?
  • What do I say are my $ 1 and $ 2 and so on?

    I do not even know what is said to them. Has anyone explained this to me?

    One replacement without errors:

      $ bibilink = ' [Url = http: //google.com] '; $ Pattern = '~ \ [url = (http: // [^]] +)] ~'; $ Replacement = '& lt; A href = "$ 1" & gt; $ 1 & lt; / A & gt; '; $ Result = preg_replace ($ pattern, $ replacement, $ bibilink); Explanation:  

    1) Pattern

      ~ # pattern delimiter \ [# Textual opening square bracket URL = (# Capturing first Group http: // [^]] + # all letters that are not one place or one or more times # Close the capturing group # Textual ending square bracket ~ # pattern delimiter   

    2) Replacement

    $ 1 First refer to the capturing group

    Optional :, see the first example.

  • No comments:

    Post a Comment