473,379 Members | 1,317 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,379 software developers and data experts.

removing whitespace from a string?

Hi,
Here is the code I use to change whitespace from a string $test to
hypen -.

<?Php

$test="Teststring test test hay man test test test test";

// Chr(32) is ASCI code for whitespace and Chr(45) is for -

$test = str_replace(Chr(32), Chr(45), $test);

echo $test;
?>

My problem is I have to take out the whitespace completely form the
string.

I will be thankful if anyone can help me, or guide me to the function
name that can be used for this.

Thank You for help.

Jul 17 '05 #1
5 3219
On 5 Mar 2005 04:33:16 -0800, sm***********@gmail.com wrote:
Hi,
Here is the code I use to change whitespace from a string $test to
hypen -.

<?Php

$test="Teststring test test hay man test test test test";

// Chr(32) is ASCI code for whitespace and Chr(45) is for -

$test = str_replace(Chr(32), Chr(45), $test);

echo $test;
?>

My problem is I have to take out the whitespace completely form the
string.


Do exactly the same, but replace it with empty strings.

$test = str_replace(chr(32), "", $test);

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
Hi,

Thanks Andy It worked

thanks a lot

bye

Jul 17 '05 #3
.oO(Andy Hassall)
Do exactly the same, but replace it with empty strings.

$test = str_replace(chr(32), "", $test);


Why the chr() function? Why no simply:

$test = str_replace(' ', '', $test);

Micha
Jul 17 '05 #4
On Sun, 06 Mar 2005 21:19:49 +0100, Michael Fesser <ne*****@gmx.net> wrote:
.oO(Andy Hassall)
Do exactly the same, but replace it with empty strings.

$test = str_replace(chr(32), "", $test);


Why the chr() function? Why no simply:

$test = str_replace(' ', '', $test);


Well, I just copied the OP's code which had the chr function, but how's this;
if all you want to do is replace the space character chr(32), can you see the
difference between:

$test = str_replace(' ', '', $test);
$test = str_replace('*', '', $test);

One of these does not do what you think :-) (Provided it survives the
copy&paste&post process...)

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #5
.oO(Andy Hassall)
Well, I just copied the OP's code which had the chr function, but how's this;
if all you want to do is replace the space character chr(32), can you see the
difference between:

$test = str_replace(' ', '', $test);
$test = str_replace('*', '', $test);

One of these does not do what you think :-)


I knew something like that would come up. ;)

But I assume the OP knows what he's doing and what he really wants to
replace. He should be able to hit the right key(s).

Micha
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Thomas Mlynarczyk | last post by:
Which is the simplest way to remove all whitespace from a string? Is there a simpler method than a regex replace? Or how can I tell a regex pattern to ignore all whitespace in my subject string?...
0
by: haughki | last post by:
apologies for asking this. it must have been answered before. i have really tried to find the answer elsewhere. a simple question: i'd like to remove all ignorable whitespace from my minidom...
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
3
by: Ray Stevens | last post by:
Does anyone know of a Regex expression from removing extra whitespace within a string (such as 5 spaces instead of 1, etc.)?
3
by: David Pratt | last post by:
Hi. I am splitting a string on a non whitespace character. One or more whitespace characters can be returned as items in the list. I do not want the items in the list that are only whitespace (can...
8
by: starsky51 | last post by:
I'm sure it's something i'm doing wrong, I just can't see it. I've set up a simple page with the following code: <html> <head> <title>tester</title> <script language="javascript"...
2
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.