By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,588 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,588 IT Pros & Developers. It's quick & easy.

preg_replace help

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.