Forum Moderators: coopster

Message Too Old, No Replies

replace space with underscore in a string

         

skoff

3:37 am on Jun 27, 2010 (gmt 0)

10+ Year Member



Everything is in the title. How can i replace a space with a underscore in a string?

Thanks a lot! :D

JAB Creations

5:48 am on Jun 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Modify this as you need...

- John

function xhtml_entities($text0)
{
$bb1 = array('&');
$xml1 = array('&');

$bb2 = array('<', '>');
$xml2 = array('&#60;','&#62;');

$text1 = str_ireplace($bb1, $xml1, $text0);
$text2 = str_ireplace($bb2, $xml2, $text1);

return $text2;
}

astupidname

4:53 am on Jun 27, 2010 (gmt 0)

10+ Year Member



Do you want all spaces converted? If so:
$str = 'a string with spaces';
$str = preg_replace("/ /", '_', $str);
echo $str;

Matthew1980

9:15 am on Jun 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I did this just last night! This does the job fine:-

Str_replace(" ","_", $data);

That returns your string as you would expect it too :)

Cheers,
MRb

Readie

11:33 am on Jun 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may be wise to prevent the input containing underscores here too - as you won't be able to convert it back.

$string = str_replace(array('_', ' '), array('', '_'), $input);


Although if I'm honest I'm not sure that won't cause issues... May be best to do:

$string = str_replace('_', '', $input);
$string = str_replace(' ', '_', $string);

skoff

2:51 pm on Jun 27, 2010 (gmt 0)

10+ Year Member



THANK YOU ALL! :D