473,507 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5289
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4885
by: Juha Suni | last post by:
Hi! I have managed to live without using too much regular expressions so far, and now that I need one, I need some help too. I have a string containing a (possibly large) block of html. I need...
4
886
by: Alexander Ross | last post by:
I dont think I'll ever understand regular expressions ... I need to do th efollowing and I just don't know where to start: $haystack = "How much wood would a wood chuck chuck if a woodchuck could...
3
4313
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str'...
9
7621
by: Sugapablo | last post by:
I admit, I'm terrible creating reg ex's. I'm trying to create a preg_replace that would remove from a <a href> tag that would replace the target attribute regardless of what the value might be....
3
5305
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
0
1559
by: temp | last post by:
Hi, I'm not good in regexp. TEXT: <a href="index.php">asasa</a> <a taget href='index.php'>asasas</a> I want to change only "index.php" in all occurance in a link tag.
3
1516
by: Trammel | last post by:
Im accepting user input from a text-box on a webpage. I am then wanting to echo that back into the same box when the page is submitted. The problem I have is that the input COULD contain...
1
1288
by: K. | last post by:
Hello! I would like to use preg_replace function to leave in the result variable only: 1) letters from A to Z including Polish letters like ±¶¼³óñê including Polish upper letters like ¡¦¬£ÓÑÊ...
4
215
by: William Gill | last post by:
I am not well versed on regular expressions, but have fumbled through enough to come up with these three preg_replace() statements (I'm not even sure how the third one works, but it does)to reduce...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.