468,537 Members | 1,844 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

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

Jul 17 '05 #1
3 5045
JDJones wrote:
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


isn't str_replace better?

<?php
$thing = str_replace(array("\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.
Jul 17 '05 #2
In article <7IDcb.429267$Oz4.226662@rwcrnsc54>,
JDJones <se******@sprynet.com> wrote:
$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


<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
Jul 17 '05 #3
Pedro wrote:
JDJones wrote:
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

isn't str_replace better?

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


*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.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Juha Suni | last post: by
4 posts views Thread by Alexander Ross | last post: by
9 posts views Thread by Sugapablo | last post: by
3 posts views Thread by Charles | last post: by
3 posts views Thread by Trammel | last post: by
1 post views Thread by K. | last post: by
4 posts views Thread by William Gill | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.