473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 5279
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
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.