Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    95
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Removing extra space in string

    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

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    95
    Thanks
    20
    Thanked 0 Times in 0 Posts
    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

    PHP Code:
    $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...

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    You can't get 'Easy Rider' row with LIKE 'Ride' query. May be that's the reason you get no records

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Echo your $sql variable to see what you're getting...

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by charu.seo View Post
    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.

  • #8
    New Coder
    Join Date
    Apr 2008
    Posts
    95
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by poyzn View Post
    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?

  • #9
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by ksduded View Post
    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.

    Code:
    $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';

  • #10
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,304
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Quote Originally Posted by ksduded View Post
    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.
    Dave .... HostMonster for all of your hosting needs

  • #11
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    you may run query to trim all column values.
    PHP Code:
    "UPDATE tablename SET `columnname` = TRIM(`columnname`)" 

  • #12
    New Coder
    Join Date
    Apr 2008
    Posts
    95
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    'LIKE %Easy Rider%' will not return ride as a match. It isn't.

    Code:
    $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.

  • #13
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by ksduded View Post
    I am sorry it is the other way around. %Ride% picks up Easy Rider.
    That's working as it should then.

  • #14
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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:
    PHP Code:
    $sql 'SELECT ... WHERE LOWER( prog_programname ) LIKE LOWER( \'%' trim$string ) . '%\' ) ORDER BY prog_date ASC'
    Last edited by kbluhm; 11-19-2010 at 08:50 PM.

  • #15
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by kbluhm View Post
    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.
    Last edited by MattF; 11-19-2010 at 08:54 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •