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

How do I replace a '_' with a space ?

VS
Hi,

I'm new to PHP, I'm having trouble finding a way to replace a '_' in a
string with a space.

i.e using 'eregi_replace' I want to change

'United_Kingdom' to

'United Kingdom'

--
VS
Oct 16 '05 #1
12 10790
$string = "United_Kingdom";
$string = str_replace ("_", " ", $string);

Have fun ;)

Oct 16 '05 #2
$my_string = "United_Kingdom";
$my_new_string = str_replace("_", " ", $my_string);
echo "$my_new_string"; //this will output United Kingdom

When you use str_replace(), you need to give it 3 elements. The first is the
character you want to replace, the second is the character to replace it
with, and the third is the string you want to modify.

You can find more info on it at www.php.net/str_replace
--
-Brandon Rohde

"VS" <vs@nospam.blueyonder.invalid> wrote in message
news:b%******************@fe3.news.blueyonder.co.u k...
Hi,

I'm new to PHP, I'm having trouble finding a way to replace a '_' in a
string with a space.

i.e using 'eregi_replace' I want to change

'United_Kingdom' to

'United Kingdom'

--
VS

Oct 16 '05 #3
Hello!
echo "$my_new_string"; //this will output United Kingdom


Why do you use
echo "$my_new_string";
instead of
echo $my_new_string;
?
Greetings,
Hero Wanders
Oct 16 '05 #4
Well, using the method without quotes is obviously fine for just printing
the variable. It's late at night, so I added the quotes by habit :)

--
-Brandon Rohde

"Hero Wanders" <he*******@nexgo.de> wrote in message
news:di*************@news.t-online.com...
Hello!
echo "$my_new_string"; //this will output United Kingdom


Why do you use
echo "$my_new_string";
instead of
echo $my_new_string;
?
Greetings,
Hero Wanders

Oct 16 '05 #5
> It's late at night, so I added the quotes by habit :)

Hehe, in Poland that's two o'clock :P

Oct 16 '05 #6
Hello!
Well, using the method without quotes is obviously fine for just printing
the variable. It's late at night, so I added the quotes by habit :)


Hmm, i _never_ use "$foo".
Concatenation is done by using the dot:
$foo.$bar.$baz (not "$foo$bar$baz").

Please explaing your habit :-)

Greetings,
Hero Wanders
Oct 17 '05 #7
> $string = "United_Kingdom";
$string = str_replace ("_", " ", $string);


couldn't you also use preg_replace()? would that be faster?
Oct 18 '05 #8
Oliver Saunders said the following on 18/10/2005 18:06:
$string = "United_Kingdom";
$string = str_replace ("_", " ", $string);

couldn't you also use preg_replace()? would that be faster?


You could preg_replace(), but I would imagine it would be orders of
magnitude slower, because regex-based matching is far more complex than
simple sub-string matching.

--
Oli
Oct 18 '05 #9
If Brandon is like me, he took the habit from frequently doing things
like

echo "Hi, my name is $name and my age is $age!";

Greetings

Oct 19 '05 #10
$string = str_replace ("United_Kingdom", "United Kingdom", $string);

is that what you want?

/f3l

Oct 19 '05 #11
Samuel wrote:
If Brandon is like me, he took the habit from frequently doing things
like

echo "Hi, my name is $name and my age is $age!";

Greetings

Please leave enough of the original post intact so that those with
clients who expire posts faster than others know what the context is.

Just because this is the habit you picked up, it does not make it a good
habit to have. Why would you have php do the contatenation and provide
additional overhead when you can do it yourself? If it's a matter of
laziness, then you should rethink your profession.

-Joe
Oct 20 '05 #12
>Just because this is the habit you picked up, it does not make it a good
habit to have. Why would you have php do the contatenation and provide
additional overhead when you can do it yourself? If it's a matter of
laziness, then you should rethink your profession.


Whoa. You just read a casual comment and in 50 words jumped to the
conclussion that I should sit back and reconsider my entire
professional life. That's got to be a new world record or something,
specially considering I never said it was a good habit...

Oct 20 '05 #13

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

Similar topics

2
by: Little PussyCat | last post by:
Hello, I need to be able to replace only the first occurance of a space character in a column. Reason being is the data in the column I am trying to replace seems to have umpteen space...
3
by: Epetruk | last post by:
If I have a snippet of javascript code like this: var str = "This is a snippet; str.replace(/ */gi,' '); what should the call to replace do? My actual tests show that it inserts spaces...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
3
by: Jim | last post by:
Hey all, I have a dilemma. I have a field in a table called "Supervisor." The table currently has 1,500+ records. Sometimes, the HR download that populates the field formats the field one way,...
4
by: Vic | last post by:
Hi, With VBA code from MSAccess, how to search for a string in another text file, and replace it with space and then save it under the same name? Your help is greatly appreciated. Rgds
4
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to...
3
by: channu | last post by:
Hi I am beginner in c++ coding.I want to read a text file and replace commas and space with tabs. Can any buddy help me out. Thanks Channu
3
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
8
by: Joe Cool | last post by:
I need to map several columns of data from one database to another where the data contains multiple spaces (once occurance of a variable number or spaces) that I need to replace with a single...
5
by: Stijn VA | last post by:
Okee, the story goes like this: I have a string that can not contain spaces (It is retrieved from a externat application...) At first they replaced the spaces by '_' so somthing like "ABC DEF"...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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...

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.