preg_replace help

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

    preg_replace help

    I'm having some trouble getting this replacement to work efficiently. I
    have the following code:

    $thing = preg_replace("( \r\n|\n|\r|\t)" , "", $thing1);

    What I want to do now is to get it to remove any semi-colons in the
    string as well as replace the new line, carriage return and tab
    delimiters. I can't seem to find a way to make the one preg_replace
    expression do both. I've had to rely on a second independent
    preg_replace statement on $thing to get it to do the semi-colons properly.

    Is there a way to combine it all into one statement? Thanks

  • Pedro

    #2
    Re: preg_replace help

    JDJones wrote:[color=blue]
    > I'm having some trouble getting this replacement to work efficiently. I
    > have the following code:
    >
    > $thing = preg_replace("( \r\n|\n|\r|\t)" , "", $thing1);
    >
    > What I want to do now is to get it to remove any semi-colons in the
    > string as well as replace the new line, carriage return and tab
    > delimiters. I can't seem to find a way to make the one preg_replace
    > expression do both. I've had to rely on a second independent
    > preg_replace statement on $thing to get it to do the semi-colons properly.
    >
    > Is there a way to combine it all into one statement? Thanks
    >[/color]

    isn't str_replace better?

    <?php
    $thing = str_replace(arr ay("\n", "\r", "\t", ';'), '', $thing1);
    ?>

    HTH

    --
    I have a spam filter working.
    To mail me include "urkxvq" (with or without the quotes)
    in the subject line, or your mail will be ruthlessly discarded.

    Comment

    • CC Zona

      #3
      Re: preg_replace help

      In article <7IDcb.429267$O z4.226662@rwcrn sc54>,
      JDJones <seebelow@spryn et.com> wrote:
      [color=blue]
      > $thing = preg_replace("( \r\n|\n|\r|\t)" , "", $thing1);
      >
      > What I want to do now is to get it to remove any semi-colons in the
      > string as well[/color]

      <snip>

      Your expression works at the moment, but I suspect that's only by accident.
      You'd intended those parentheses for grouping, not delimiters, right? DON'T
      FORGET THAT PREG_ EXPRESSIONS REQUIRES REGEX DELIMITERS (most commonly a
      pair of forward slashes). i.e.

      $thing = preg_replace("/[\r\n\t;]/", "", $thing1);

      If you're concerned about execution time, you may want to benchmark this
      against the str_replace suggested by the other poster. Preg_ is generally
      pretty fast, but str_replace might well be the faster in this case.

      --
      CC

      Comment

      • JDJones

        #4
        Re: preg_replace help

        Pedro wrote:[color=blue]
        > JDJones wrote:
        >[color=green]
        >>I'm having some trouble getting this replacement to work efficiently. I
        >>have the following code:
        >>
        >>$thing = preg_replace("( \r\n|\n|\r|\t)" , "", $thing1);
        >>
        >>What I want to do now is to get it to remove any semi-colons in the
        >>string as well as replace the new line, carriage return and tab
        >>delimiters. I can't seem to find a way to make the one preg_replace
        >>expression do both. I've had to rely on a second independent
        >>preg_replac e statement on $thing to get it to do the semi-colons properly.
        >>
        >>Is there a way to combine it all into one statement? Thanks
        >>[/color]
        >
        >
        > isn't str_replace better?
        >
        > <?php
        > $thing = str_replace(arr ay("\n", "\r", "\t", ';'), '', $thing1);
        > ?>
        >[/color]

        *head hung low*
        I was under the impression that it had to be done as a preg_replace. But
        I now see that your str_replace works just as well. One of these days I
        may actually get all these PHP things to sink in.

        Thank you and CC for the help. I have changed it to the str_replace.

        Comment

        Working...