Replacing Spaces with Underscores

Php

Php Problem Overview


I have a PHP Script that users will enter a name like: Alex_Newton,

However, some users will use a space rather than an underscore, so my question is:

How do I auto-replace spaces with Underscores in PHP?

Php Solutions


Solution 1 - Php

$name = str_replace(' ', '_', $name);

Solution 2 - Php

As of others have explained how to do it using str_replace, you can also use regex to achieve this.

$name = preg_replace('/\s+/', '_', $name);

Solution 3 - Php

$name = str_replace(' ', '_', $name);

http://php.net/manual/en/function.str-replace.php

Solution 4 - Php

Use str_replace function of PHP.

Something like:

$str = str_replace(' ', '_', $str);

Solution 5 - Php

Call http://php.net/str_replace: $input = str_replace(' ', '_', $input);

Solution 6 - Php

Use str_replace:

str_replace(" ","_","Alex Newton");

Solution 7 - Php

You can also do this to prevent the words from beginning or ending with underscores like words_more_words, This would avoid beginning and ending with white spaces.

$trimmed = trim($string); // Trims both ends
$convert = str_replace('', '_', $trimmed);

Solution 8 - Php

you can use str_replace say your name is in variable $name

$result = str_replace(' ', '_', $name);

another way is to use regex, as it will help to eliminate 2-time space etc.

  $result=  preg_replace('/\s+/', '_', $name);

Solution 9 - Php

This is part of my code which makes spaces into underscores for naming my files:

$file = basename($_FILES['upload']['name']);
$file = str_replace(' ','_',$file);

Solution 10 - Php

I used like this

$option = trim($option);
$option = str_replace(' ', '_', $option);

Solution 11 - Php

Strtr replaces single characters instead of strings, so it's a good solution for this example. Supposedly strtr is faster than str_replace (but for this use case they're both immeasurably fast).

echo strtr('Alex Newton',' ','_');
//outputs: Alex_Newton

Solution 12 - Php

str_replace - it is evident solution. But sometimes you need to know what exactly the spaces there are. I have a problem with spaces from csv file.

There were two chars but one of them was 0160 (0x0A0) and other was invisible (0x0C2)

my final solution:

$str = preg_replace('/\xC2\xA0+/', '', $str);

I found the invisible symbol from HEX viewer from mc (midnight viewer - F3 - F9)

Solution 13 - Php

if you have a parameter $string and want to replace it's apace to underscore(_).

$string = "Hello All.";
$newString = str_replace(' ', '_', $string);

output will be Hello_All.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexView Question on Stackoverflow
Solution 1 - PhpTim FountainView Answer on Stackoverflow
Solution 2 - PhpaksuView Answer on Stackoverflow
Solution 3 - PhpViktorView Answer on Stackoverflow
Solution 4 - PhpanubhavaView Answer on Stackoverflow
Solution 5 - PhpwebspyView Answer on Stackoverflow
Solution 6 - PhpNiklasView Answer on Stackoverflow
Solution 7 - PhpblakrokuView Answer on Stackoverflow
Solution 8 - PhpRajView Answer on Stackoverflow
Solution 9 - PhpjmmaguigadView Answer on Stackoverflow
Solution 10 - PhpFilView Answer on Stackoverflow
Solution 11 - PhpThoracius AppotiteView Answer on Stackoverflow
Solution 12 - PhpAleksey PolyanskiyView Answer on Stackoverflow
Solution 13 - PhpAshutosh NiranjanView Answer on Stackoverflow