...

View Full Version : Removing extra space in string



ksduded
11-18-2010, 09:32 PM
some of my strings have a lot of empty space

e.g. "This is my string "

I want to remove the empty space after the g in string so that it reads

"This is my string"

How can i achieve this?

thanks

kbluhm
11-18-2010, 09:33 PM
http://www.php.net/trim
http://www.php.net/ltrim
http://www.php.net/rtrim

ksduded
11-18-2010, 09:59 PM
http://www.php.net/trim
http://www.php.net/ltrim
http://www.php.net/rtrim

i tried that. but for some reason it is not working for me. I think it has to do with the code proceeding...

I have strings which have a lot of white space after the string. So from what I understand I get rid of the extra space by

$string = trim($string);

after that I am running a sql query


$sql = "SELECT prog_time, prog_date, prog_timezone, prog_programname, prog_episodename, prog_episodedesc FROM rushhd_schd WHERE prog_programname LIKE '%{$string}%' ORDER BY prog_date ASC";

now I want to get rid of the % signs so that I can compare it accurately, for example I have a string 'Easy Rider'. And when I run it with %{$string}%, it even picks up records from the string 'Ride'.

So naturally I remove the % and make it {$string}, but then I get no records...

poyzn
11-19-2010, 10:12 AM
You can't get 'Easy Rider' row with LIKE 'Ride' query. May be that's the reason you get no records

charu.seo
11-19-2010, 10:16 AM
Removes extra space from the string using Regular expression.

public static readonly RegexOptions Options = RegexOptions.IgnorePatternWhitespace |
RegexOptions.Singleline;
public string RemoveExtaSpaces(string text)
{
Regex regex = new Regex(@”\s{2,}”, Options);
text = regex.Replace(text.Trim(), ” “); //This line removes extra spaces and make space exactly one.
//To remove the space between the end of a word and a punctuation mark used in the text we will
//be using following line of code
regex=new Regex(@”\s(\!|\.|\?|\;|\,|\:)”); // “\s” whill check for space near all puntuation marks in side ( \!|\.|\?|\;|\,|\:)”); )
text = regex.Replace(text, “$1″);
return text;

}

Above function removes all extra spaces in the string to make one space exactly.

Brandoe85
11-19-2010, 10:17 AM
Echo your $sql variable to see what you're getting...

kbluhm
11-19-2010, 12:39 PM
Removes extra space from the string using Regular expression.

public static readonly RegexOptions Options = RegexOptions.IgnorePatternWhitespace |
RegexOptions.Singleline;
public string RemoveExtaSpaces(string text)
{
Regex regex = new Regex(@\s{2,}, Options);
text = regex.Replace(text.Trim(), ); //This line removes extra spaces and make space exactly one.
//To remove the space between the end of a word and a punctuation mark used in the text we will
//be using following line of code
regex=new Regex(@\s(\!|\.|\?|\;|\,|\:)); // \s whill check for space near all puntuation marks in side ( \!|\.|\?|\;|\,|\:)); )
text = regex.Replace(text, $1″);
return text;

}

Above function removes all extra spaces in the string to make one space exactly.

This is a PHP forum, guy.

ksduded
11-19-2010, 03:16 PM
You can't get 'Easy Rider' row with LIKE 'Ride' query. May be that's the reason you get no records

I echoed the result and it did say Easy Rider.... but it did not pick up the records associated with Easy Rider...

I think the problem is that the entry in my database has "Easy Rider [a lot of empty space]"

so when i use no {$string} without %..... my String is just "Easy Rider" and then it cannot find a relevant entry because database has all those empty spaces after Easy Rider.....

It will be incredibly hard to change the database entries and get rid of the space manually. Is there a work around through php that I can do?

MattF
11-19-2010, 03:43 PM
for example I have a string 'Easy Rider'. And when I run it with %{$string}%, it even picks up records from the string 'Ride'.

'LIKE %Easy Rider%' will not return ride as a match. It isn't.



$sql = 'SELECT prog_time, prog_date, prog_timezone, prog_programname, prog_episodename, prog_episodedesc FROM rushhd_schd WHERE LOWER(prog_programname) LIKE \'%LOWER('.trim($string).')%\' ORDER BY prog_date ASC';

djm0219
11-19-2010, 03:44 PM
It will be incredibly hard to change the database entries and get rid of the space manually. Is there a work around through php that I can do?

If you are using MySQL look at the TRIM string function. You should be able to remove leading and/or trailing spaces from your columns with a single command. It would be far better to fix the source of the problem than trying to code around it.

poyzn
11-19-2010, 03:44 PM
you may run query to trim all column values.


"UPDATE tablename SET `columnname` = TRIM(`columnname`)"

ksduded
11-19-2010, 08:31 PM
'LIKE %Easy Rider%' will not return ride as a match. It isn't.



$sql = 'SELECT prog_time, prog_date, prog_timezone, prog_programname, prog_episodename, prog_episodedesc FROM rushhd_schd WHERE LOWER(prog_programname) LIKE \'%LOWER('.trim($string).')%\' ORDER BY prog_date ASC';


I am sorry it is the other way around. %Ride% picks up Easy Rider.

MattF
11-19-2010, 08:36 PM
I am sorry it is the other way around. %Ride% picks up Easy Rider.

That's working as it should then.

kbluhm
11-19-2010, 08:47 PM
Really? LOWER() is working within the single quotes? That will treat the string as though it starts with `LOWER(` and ands with `)`, won't it?

I would have it like so:


$sql = 'SELECT ... WHERE LOWER( prog_programname ) LIKE LOWER( \'%' . trim( $string ) . '%\' ) ORDER BY prog_date ASC';

MattF
11-19-2010, 08:50 PM
Really? LOWER() is working within the single quotes? That will treat the string as though it starts with `LOWER(` and ands with `)`, won't it?

I think he was just referring to his earlier mistake, not the query I posted, and yup. Definite bollock drop on my part there. :D

kbluhm
11-19-2010, 09:13 PM
Ah, gotcha... yeah, figured it must have been a typo. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum