Is there a reason that I'm not seeing, why this doesn't work?
$string = $someLongUserGeneratedString; $replaced = str_replace(' ', '_', $string); echo $replaced;
The output still includes spaces... Any ideas would be awesome
if you have a parameter $string and want to replace it's apace to underscore(_). $string = "Hello All."; $newString = str_replace(' ', '_', $string);
Answer: Use the PHP str_replace() Function You can simply use the PHP str_replace() function to strip or remove all spaces inside a string.
Use the String. replaceAll method to replace all spaces with underscores in a JavaScript string, e.g. string. replaceAll(' ', '_') . The replaceAll method returns a new string with all whitespace characters replaced by underscores.
I'll suggest that you use this as it will check for both single and multiple occurrence of white space (as suggested by Lucas Green).
$journalName = preg_replace('/\s+/', '_', $journalName);
instead of:
$journalName = str_replace(' ', '_', $journalName);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With