Jump to content

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>';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.