Problems trimming whitespace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Problems trimming whitespace

Post by mudvein »

Guys, I've tried doing this so many different ways that my mind is now about to explode. I have a qury that, when I print it out, looks on my screen as follows :

Code: Select all

SELECT id, count(name) as total_names from NMname where name = '  

Steve' and id = '0' group by id
If you notice, There is a huge space and then some carriage returns in there. Now, I've tried trim, ltrim, preg_replace, ereg_replace, str_replace, and NONE of these things can get rid of this whitespace! I'm beginning to go mad trying to figure this out!

I've even went so far as to print out the actual variable that is holding the name value (strName).

It prints out, you guessed it, with the spaces and new line chars as well. I am completely lost with wonder here... any kind person that would like to help me would be much appreciated if they have come across this same issue...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

trim() should work just fine... maybe posting the code you tried?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: Problems trimming whitespace

Post by neophyte »

mudvein wrote:If you notice, There is a huge space and then some carriage returns in there. Now, I've tried trim, ltrim, preg_replace, ereg_replace, str_replace, and NONE of these things can get rid of this whitespace!
White space in the query text? Post code please...
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

Code: Select all

function checkit($strName) {
   $strName = trim($strName);
   $check = "SELECT id, count(name) as total from NMname 
    where name = '".$strName."' and id = '0' group by id";
   echo '<br />'.$check.'<br />';
   echo 'The value of string is '.$strName.'<br />';
   //mysql_query($check);
   //$row = mysql_fetch_assoc($mysql_query);
}
as I say, I've tried using str_replace, eregi, trim, ltrim, etc... but nothing gets the whitespace out of that string... the string itself is coming from a file that I have parsed, so I don't know if there is some sort of special space character key one must use or what...but it's definately not liking me or what i'm attempting with it.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Error message?
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

so i guess my main question is, is there any way to tell php to display ALL characters as some thing other than invisible space? That way i could at least try to figure out what the hell is going on... Wether it's a crazy mixed thing like &#NA that represents a space, or whatever ? Dunno the proper term for this..
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

no error message at all..


Edit : I guess I mean, is there a way to display hidden characters as a human readable format (ie : a ' ' would be represented as %s, a ' ' tab would be represented as '%t', etc...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

since you are echoing the stuff out into HTML, htmlentities() would be useful..
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

k...maybe i shoulda tried this before.. firefox is not displaying any whitespace. maybe it's just ie's problem with css.. who knows. im going to bed. wasted 3 hours of my time on this bs problem.


ty feyd.
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

last question. what would <p> mean??? that is what IE is showing me now...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<p> is a HTML tag for "paragraph"
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

well, when i used htmlentities, it said a <p> was before the char. And I don't know how to tell ereg or str_replace to search for a paragraph character.. any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's not a paragraph character. It's an HTML tag which, technically, is completely valid data. If you want to remove HTML from the input, use strip_tags() or the code I posted a while ago making a smarter strip_tags() (linked to from Useful Posts, link in my signature)
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

what can i say feyd, ur the man. strip tags worked like a charM! it worries me though, because when I echod the string out and viewed the source, there was no tags at all (not even in the file itself). Oh well, thanks muchos =D
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

use mysql_real_escape_string() on $strName before using it in the query too.
Post Reply