473,666 Members | 2,073 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 5308
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(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.
Jul 17 '05 #2
In article <7IDcb.429267$O z4.226662@rwcrn sc54>,
JDJones <se******@spryn et.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_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

isn't str_replace better?

<?php
$thing = str_replace(arr ay("\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
4895
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 to insert code (an image to be precise) right after the last textual character in the string. This is, of course, no problem if the end contains plain text and no html. If the string ends in HTML however, I need to fiddle around. for example...
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 chuck wood?"; $needles = ("wood","chuck","wood chuck"); I want to replace every needle with an anchor tag like this:
3
4317
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' which may contain new lines and will contain html code, IN this string any "words" that begin with an underscore I want to replace with a given word. A word here being a group of chars preceded by a space or null (start of line) and closed by a...
9
7673
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. Any ideas? --
3
5321
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 --- Sig line1 Sig line2 Sig line3 If I could just get rid of that, it would be pretty good. But I also get this
0
1569
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
1529
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 multiple quotes and double-quotes. AddSlashes doesnt seem to work properly, as it turns ' into \' ....and " just into \ (removing everything after) I thought of using single-quotes for the Value='' and then use preg_replace
1
1296
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 ¡¦¬£ÓÑÊ 2) letters ()-_=+!@#$%^&*`~',/\|. I have for example such varaible
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 a form field value to the '(###) ###-####' U.S. phone format. My question, can I combine them into one? $phone=preg_replace('/\D/','',$text); // strip all non-digits $phone=preg_replace('/^/','',$text); // strip leading 0 or 1...
0
8448
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8783
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8552
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5666
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.