Forum Moderators: coopster

Message Too Old, No Replies

Remove white space between 2 words

Not trim() but in between words

         

henry0

10:02 pm on Aug 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to find a way to remove space in between two words
Example:
aa bb
Should read: aabb
So if I have $test="aa bb";
Where do I go from there?
I tried unsuccessfully

$test="aa bb";
Send= " /\"\" "; ( to add the /"" that stands for rem white space)

$test2=$test.$end;
echo chop($test2);

Which results in
aa bb /""

Incidentally is it correct/possible to have a table named with space in between two words
I tried on MySQL 5.0.22 on my local test bed and it was accepted!
I did not think it was valid?

barns101

10:13 pm on Aug 17, 2006 (gmt 0)

10+ Year Member



Unless I'm missing something, implode [php.net] is exactly what you're looking for.

(Can't held with the MySQL question, but I've always assumed you shouldn't use spaces.)

henry0

10:46 pm on Aug 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did not think about implode() :)
will give it a try

whoisgregg

3:02 pm on Aug 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you trying to remove all white space?

$input = " aa bb ";
$output = str_replace(" ","",$input);
echo $output; // aabb

henry0

7:27 pm on Aug 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, Yes I had a battle with implode() and concluded it won't do the job
The correct question should have been "remove all white spaces"
This will work!
I am working on a system where a user (The web owner) creates a new dir, new files and new rows by entering a new dir name formed out of two separated words (Those names are also used as is in the nav bar).
To make my system more "readable" I had to find a way of combining the two words in only one to name a new auto generated table (related to the new dir) named by using the combo.

thanks again that will do fine.

whoisgregg

8:20 pm on Aug 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Typically, I would do something like this for user supplied url data:

function makeURLFriendly($input){
$output = strtolower($input);
$output = preg_replace("/[^[:space:]a-z0-9]/e", "", $output);
$output = trim($output);
$output = preg_replace('/\s\s+/', '_', $output);
return $output;
}//function makeURLFriendly($input){
echo $input = ' My Directory &   Stuff ';
echo '<br />';
echo makeURLFriendly($input); // my_directory_stuff

henry0

10:52 pm on Aug 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



strtolower id a good addition
I am guilty to not use it enough

My approach is a tad different in that I am filtrating input from the $_POST step
Using!preg_match to guaranty that it will look as required.

Well actually I first use a $clean() to be on the safe side
Then preg()
And using if {} I send the user back to the form if empty, unsafe, unauthorized char etc.. but I create a session that allows to form data persistence
And kill the script next step using exit()

<edit>
New to PHP:
The function clean()
means a home made () not in the manual :)
That tries to keep input clean of attack
</edit>

coopster

3:56 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




Incidentally is it correct/possible to have a table named with space in between two words
I tried on MySQL 5.0.22 on my local test bed and it was accepted!
I did not think it was valid?

You can have spaces, but database, table, and column names should not end with space characters. The bigger question is why would you though? It's just so much easier to not have to be concerned with quoting identifiers when they have special characters. An identifier may be quoted or unquoted. If an identifier is a reserved word or contains special characters, you must quote it whenever you refer to it.
Special characters are those outside the set of alphanumeric characters from the current character set, ‘_’, and ‘$’.

[dev.mysql.com...]