preg_replace question

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

    preg_replace question

    I'm fairly new to regex code, and I'm trying to find a way to replace
    any text after "///" (three slashes) and before a "\n" character.


    I've tried with:

    $filen = preg_replace("///./\n$", "", $filen);
    The pattern would mean:
    "string that has three "/" followed by any characters and which ends
    with \n"


    $filen = preg_replace("//{2}.\n$", "", $filen);
    The pattern would mean:
    "string that has one "/" followed two "/", then followed by any
    character and which ends with \n"


    What's wrong with these patterns?

    regards - julian

  • Rik

    #2
    Re: preg_replace question

    julianmlp@gmail .com wrote:[color=blue]
    > I'm fairly new to regex code, and I'm trying to find a way to replace
    > any text after "///" (three slashes) and before a "\n" character.
    >
    >
    > I've tried with:
    >
    > $filen = preg_replace("///./\n$", "", $filen);
    > The pattern would mean:
    > "string that has three "/" followed by any characters and which ends
    > with \n"
    >
    >
    > $filen = preg_replace("//{2}.\n$", "", $filen);
    > The pattern would mean:
    > "string that has one "/" followed two "/", then followed by any
    > character and which ends with \n"
    >
    >
    > What's wrong with these patterns?[/color]

    1. The first character (in this case '/') is considered the pattern
    delimiter, which never closes here...
    2. . should match zero or more characters I presume?
    3. $ will match only the end on the string without any modifiers, not every
    newline.

    Possibilities:
    $filen = preg_replace("|/{3}.*?$|m", "", $filen);
    $filen = preg_replace("|/{3}.*?\n|", "", $filen);

    Grtz,
    --
    Rik Wasmus


    Comment

    • lorento

      #3
      Re: preg_replace question

      Try this :

      <?php
      $str = "Hello World ///World Hello\n";
      $str .= "Helllooooo o ///allohhhaaaa\n";

      echo "<b>Before : </b><br>$str<br>< br>";

      echo "<b>After:</b><br>";
      echo preg_replace ("/\/\/\/(.*)/", "", $str);

      ?>

      regards,

      Lorento
      --




      Comment

      • julian_m

        #4
        Re: preg_replace question


        lorento wrote:[color=blue]
        > Try this :
        >
        > <?php
        > $str = "Hello World ///World Hello\n";
        > $str .= "Helllooooo o ///allohhhaaaa\n";
        >
        > echo "<b>Before : </b><br>$str<br>< br>";
        >
        > echo "<b>After:</b><br>";
        > echo preg_replace ("/\/\/\/(.*)/", "", $str);
        >
        > ?>[/color]


        Great. Just a small detail which I cant't achieve even though both you
        and Rik gave me excelent advice:

        Your code output is:

        After:
        Hello World Hellloooooo

        whereas I would like to get

        Hello World
        Hellloooooo

        So I thought it would be esay to solve just changing[color=blue][color=green][color=darkred]
        >>>echo preg_replace ("/\/\/\/(.*)/", "", $str);[/color][/color][/color]
        to[color=blue][color=green][color=darkred]
        >>>echo preg_replace ("/\/\/\/(.*)/", "/n", $str);[/color][/color][/color]

        Note the "/n"
        But it actually doesn't work at all.
        What's wrong? Why "/n" doesn't do anything?

        regards - julian

        Comment

        • Rik

          #5
          Re: preg_replace question

          Rik wrote:[color=blue]
          > Possibilities:
          > $filen = preg_replace("|/{3}.*?$|m", "", $filen);
          > $filen = preg_replace("|/{3}.*?\n|", "", $filen);[/color]

          julian_m wrote:[color=blue]
          > Your code output is:
          >
          > After:
          > Hello World Hellloooooo
          >
          > whereas I would like to get
          >
          > Hello World
          > Hellloooooo[/color]


          $filen = preg_replace("|/{3}.*?$|m", "\n", $filen);
          $filen = preg_replace("|/{3}.*?\n|", "\n", $filen);

          or:[color=blue][color=green][color=darkred]
          >>>echo preg_replace ("/\/\/\/(.*)/", "\n", $str);[/color][/color][/color]

          Not "/n" :-)

          Grtz,
          --
          Rik Wasmus


          Comment

          • julian_m

            #6
            Re: preg_replace question


            Rik wrote:
            [color=blue][color=green][color=darkred]
            > >>>echo preg_replace ("/\/\/\/(.*)/", "\n", $str);[/color][/color]
            >
            > Not "/n" :-)
            >[/color]

            I don't know what I was thinking about ;)

            Thanks Rik.

            regards - julian

            Comment

            Working...