Jump to content

[SOLVED] How would you trim excess spaces inside a string?


limitphp

Recommended Posts

I know that trim will remove any spaces before or after a string, but what if I wanted to remove any excess spaces inside a string?

 

For example, if I had $value = "a    b    c";

there are more than 1 spaces between the letters. 

Is there a best practice method to replace more than one space between characters with just one space?

 

Also, for mod rewrites safe urls, I know you replace spaces with dashes, what about periods and underscores, are those characters safe for a url?

 

thanks

 

thanks

Link to comment
Share on other sites

ereg_replace(" ", "", $string);

 

that should work

 

No, that removes all spaces.

 

use this

<?

$string = "this      has  lots  of    extra      spaces";

echo preg_replace('/\\s{2,}/',' ',$string);

?>

 

that worked perfectly....thank you.

 

I know dashes are used for mod rewrite safe names

ex)

mysite.com/artists/artist-name/

 

but what about underscores and periods?

 

is there a safe list of characters somewhere?

thanks

Link to comment
Share on other sites

but what about underscores and periods?

 

is there a safe list of characters somewhere?

thanks

 

Periods I would suggest against as they usually distinguish a file. Underscores are fine.

 

As far as a safe list, google "SEO best practices". To me a - is better than an _ but that is just my preference.

Link to comment
Share on other sites

but what about underscores and periods?

 

is there a safe list of characters somewhere?

thanks

 

Periods I would suggest against as they usually distinguish a file. Underscores are fine.

 

As far as a safe list, google "SEO best practices". To me a - is better than an _ but that is just my preference.

 

Well, the problem is, eventually, people will be able to view people's profile on my site....

ex)

mysite.com/users/username/

 

and right now i allow usernames to contain periods, underscores and dashes.

If those were safe characters, then I want to just use their username as the value to put in the link.

 

so technically, it could be:

ex)

mysite.com/users/my-long.user_name/

 

 

Link to comment
Share on other sites

Underscores/dashes are fine. The periods are probably fine too, I was just stating an obvious point, file names contain periods then the extension, which most search engines can interpret as so and rank you lower.

 

I would disallow the use of periods, but that is me. A username does not need periods, a "display name" can have periods/whatever it wants. It is better, if you ask me, to keep usernames simple and straight forward, Aplhanumeric.

Link to comment
Share on other sites

Underscores/dashes are fine. The periods are probably fine too, I was just stating an obvious point, file names contain periods then the extension, which most search engines can interpret as so and rank you lower.

 

I would disallow the use of periods, but that is me. A username does not need periods, a "display name" can have periods/whatever it wants. It is better, if you ask me, to keep usernames simple and straight forward, Aplhanumeric.

Ok, I think that's what I'll do.  I'll let them use dashes only in usernames, no periods or underscores.

 

thanks for the advice.

Link to comment
Share on other sites

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.