limitphp Posted February 18, 2009 Share Posted February 18, 2009 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 https://forums.phpfreaks.com/topic/145774-solved-how-would-you-trim-excess-spaces-inside-a-string/ Share on other sites More sharing options...
milesap Posted February 18, 2009 Share Posted February 18, 2009 ereg_replace(" ", "", $string); that should work Link to comment https://forums.phpfreaks.com/topic/145774-solved-how-would-you-trim-excess-spaces-inside-a-string/#findComment-765363 Share on other sites More sharing options...
Prismatic Posted February 18, 2009 Share Posted February 18, 2009 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); ?> Link to comment https://forums.phpfreaks.com/topic/145774-solved-how-would-you-trim-excess-spaces-inside-a-string/#findComment-765366 Share on other sites More sharing options...
limitphp Posted February 18, 2009 Author Share Posted February 18, 2009 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 https://forums.phpfreaks.com/topic/145774-solved-how-would-you-trim-excess-spaces-inside-a-string/#findComment-765400 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 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 https://forums.phpfreaks.com/topic/145774-solved-how-would-you-trim-excess-spaces-inside-a-string/#findComment-765407 Share on other sites More sharing options...
limitphp Posted February 18, 2009 Author Share Posted February 18, 2009 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 https://forums.phpfreaks.com/topic/145774-solved-how-would-you-trim-excess-spaces-inside-a-string/#findComment-765412 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 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 https://forums.phpfreaks.com/topic/145774-solved-how-would-you-trim-excess-spaces-inside-a-string/#findComment-765421 Share on other sites More sharing options...
limitphp Posted February 18, 2009 Author Share Posted February 18, 2009 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 https://forums.phpfreaks.com/topic/145774-solved-how-would-you-trim-excess-spaces-inside-a-string/#findComment-765470 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.