469,275 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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 10223
$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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Little PussyCat | last post: by
3 posts views Thread by Epetruk | last post: by
20 posts views Thread by hagai26 | last post: by
3 posts views Thread by Jim | last post: by
4 posts views Thread by Neo Geshel | last post: by
3 posts views Thread by channu | last post: by
3 posts views Thread by Pascal | last post: by
5 posts views Thread by Stijn VA | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.