Jump to content

How to Remove odd spaces...?


QueenZ

Recommended Posts

Hi guys,

Let's say that i have a string like this...

 

$string = "word      word     word";

or like this

$string = "word------word-----word";

 

I need them to be.

$string = "word word word";

$string= "word-word-word"

 

I'm wondering what function removes odd characters from the string. Characters that repeats...

As far as i know trim() only removes repeated chars before or after the string only.. :(

Link to comment
https://forums.phpfreaks.com/topic/121043-how-to-remove-odd-spaces/
Share on other sites

well if we assume any space >2 is bad we can try to do it with regex

 

we can use a pattern like

^(\s\s+)?$

and use preg_replace to fix it up

 

I'm not that good with this kind of stuff so i don't know how to do this.. :(

 

$string = "word      word     word";

$new_string = preg_replace('/([\s]+)/', ' ', $string);

echo '<pre>' . $string . '</pre>';
echo '<pre>' . $new_string . '</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.