Web Analytics Made Easy -
StatCounter Regular Expression - Character Replacement - CodingForum

Announcement

Collapse
No announcement yet.

Regular Expression - Character Replacement

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Regular Expression - Character Replacement

    So I am trying to use regexp to replace text but I can't get the order to function correctly.
    For example:
    Code:
    $string = '`something`something`;
    echo preg_replace('/`(.*)`/', '`test`', $string);
    This returns: `test`

    What I'd like it to return is: `test`something`

    How do you make it so that it only takes up the 1st time, rather than the last time?

    (I hope that question made sense...)

  • #2
    PHP Code:
    preg_replace'/`[^`]+`/Us''`test`'$string); 
    ZCE

    Comment


    • #3
      That sounds good. A couple of questions though,

      What does the 4th parameter do in preg_replace?
      Also, what does the /Us do?

      The other part I understood.
      Thanks!

      Comment


      • #4
        The `U` makes it ungreedy, which it is technically already doing anyhow by looking in between backticks for any characters that aren't a backtick, but it's always cool to play it safe.

        The `s` will allow the pattern to jump across new lines.

        The fourth argument is how many matches you want to match. 1 means once it finds a match it stops. If set to 2, it will stop after two matches, etc...
        ZCE

        Comment

        Working...
        X