Question about array syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sclemens420@hotmail.com

    #1

    Question about array syntax

    On the PHP site on the preg_replace() function page (
    http://us3.php.net/manual/en/function.preg-replace.php ), example 1
    uses "@" signs in the regular expression members of the "search" array.
    On another PHP site (
    http://www.phpfreaks.com/phpmanual/p...g-replace.html ),
    Example 5 is the same script except the regex array members use double-
    then single-quotes.

    Why?

    Both scripts seem to work the same. I can find no documentation on how
    the "@" sign affects how the regex is used.

    A link to explanatory documentation would be highly appreciated.

  • noone

    #2
    Re: Question about array syntax

    sclemens420@hot mail.com wrote:
    [color=blue]
    > On the PHP site on the preg_replace() function page (
    > http://us3.php.net/manual/en/function.preg-replace.php ), example 1
    > uses "@" signs in the regular expression members of the "search" array.
    > On another PHP site (
    > http://www.phpfreaks.com/phpmanual/p...g-replace.html ),
    > Example 5 is the same script except the regex array members use double-
    > then single-quotes.[/color]
    [color=blue]
    > Why?[/color]
    [color=blue]
    > Both scripts seem to work the same. I can find no documentation on how
    > the "@" sign affects how the regex is used.[/color]
    [color=blue]
    > A link to explanatory documentation would be highly appreciated.[/color]

    the "@" sign in the first example is used as the delimiter. In the second
    example the delimiter is "/"

    With regex - you can use almost any meta-character as a delimiter.



    Comment

    • Chung Leong

      #3
      Re: Question about array syntax

      sclemens420@hot mail.com wrote:[color=blue]
      > On the PHP site on the preg_replace() function page (
      > http://us3.php.net/manual/en/function.preg-replace.php ), example 1
      > uses "@" signs in the regular expression members of the "search" array.
      > On another PHP site (
      > http://www.phpfreaks.com/phpmanual/p...g-replace.html ),
      > Example 5 is the same script except the regex array members use double-
      > then single-quotes.
      >
      > Why?
      >
      > Both scripts seem to work the same. I can find no documentation on how
      > the "@" sign affects how the regex is used.
      >
      > A link to explanatory documentation would be highly appreciated.[/color]

      The @ does nothing. In accordance with PERL syntax, you can use nearly
      any non-alphanumeric characters to bracket regular expressions. People
      usually stick with the /expression/ format, for among other things,
      that's required in Javascript. Some people like to use @expression@
      because in web application, you often search for the slash character
      itself.

      The following all do the same thing:

      preg_match('|he llo|', "hello");
      preg_match('@he llo@', "hello");
      preg_match('/hello/', "hello");
      preg_match('%he llo%', "hello");
      preg_match('#he llo#', "hello");
      preg_match('<he llo>', "hello");
      preg_match('?he llo?', "hello");
      preg_match('[hello]', "hello");
      preg_match('&he llo&', "hello");
      preg_match('(he llo)', "hello");
      preg_match('=he llo=', "hello");

      Some are, of course, goofier than others.

      Comment

      Working...