preg_replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stijn Goris

    preg_replace

    Hi all,

    I m trying to create a smiley system but no luck

    I have
    preg_replace ("/:-)/", '<img src="images/smiley.gif">', $strings);
    but it returns Warning: Compilation failed: unmatched parentheses at offset
    2

    I have looked in the manual but no luck so far

    regards
    Stijn



  • Andy Hassall

    #2
    Re: preg_replace

    On Tue, 23 Mar 2004 00:27:02 GMT, "Stijn Goris" <mepisto@hotmai l.com> wrote:
    [color=blue]
    >Hi all,
    >
    >I m trying to create a smiley system but no luck
    >
    >I have
    >preg_replace ("/:-)/", '<img src="images/smiley.gif">', $strings);
    >but it returns Warning: Compilation failed: unmatched parentheses at offset
    >2
    >
    >I have looked in the manual but no luck so far[/color]

    Parentheses are ( and ).

    These are used to capture patterns.

    For each ( there must be a ).

    If you want to match a literal ( or a literal ) you must escape them.

    To escape characters you use \.

    You're not including any variables in the pattern, so use single quotes. This
    also means you can stop worrying about any confusion with PHP's escaping
    mechanism.

    preg_replace ('/:-\)/', '<img src="images/smiley.gif">', $strings);

    Since there's nothing variable in your pattern match anyway, regular
    expressions are overkill, so just use str_replace.

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

    Comment

    • Stijn Goris

      #3
      Re: preg_replace


      "Andy Hassall" <andy@andyh.co. uk> wrote in message
      news:lb2v50pp2a e96vc0dn2q8tudl 6rmi08e6f@4ax.c om...[color=blue]
      > On Tue, 23 Mar 2004 00:27:02 GMT, "Stijn Goris" <mepisto@hotmai l.com>[/color]
      wrote:[color=blue]
      >[color=green]
      > >Hi all,
      > >
      > >I m trying to create a smiley system but no luck
      > >
      > >I have
      > >preg_replace ("/:-)/", '<img src="images/smiley.gif">', $strings);
      > >but it returns Warning: Compilation failed: unmatched parentheses at[/color][/color]
      offset[color=blue][color=green]
      > >2
      > >
      > >I have looked in the manual but no luck so far[/color]
      >
      > Parentheses are ( and ).
      >
      > These are used to capture patterns.
      >
      > For each ( there must be a ).
      >
      > If you want to match a literal ( or a literal ) you must escape them.
      >
      > To escape characters you use \.
      >
      > You're not including any variables in the pattern, so use single quotes.[/color]
      This[color=blue]
      > also means you can stop worrying about any confusion with PHP's escaping
      > mechanism.
      >
      > preg_replace ('/:-\)/', '<img src="images/smiley.gif">', $strings);
      >
      > Since there's nothing variable in your pattern match anyway, regular
      > expressions are overkill, so just use str_replace.
      >
      > --
      > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]
      thanks


      Comment

      Working...