|
-
May 3rd, 2004, 02:33 PM
#1
Thread Starter
Frenzied Member
trim [Resolved]
Ok, so I'm using the trim function on a field being retrieved from a MS SQL Database. The field appears to be empty, but the following does not work.
PHP Code:
echo "<p class=warn>-" . trim($row[5]) . "-";
It simply outputs "- -"... with a space between the dashes. Any ideas why?
Last edited by ober0330; May 5th, 2004 at 08:21 AM.
-
May 3rd, 2004, 02:46 PM
#2
I am notgood at this. But you could try to put " " around the trim function like this:
PHP Code:
echo "<p class=warn>-" . "trim($row[5])" . "-";
If that doesn't work, have you tested it without the trim function. Is it actually anything there?
Or you could try to do the trimming before the echo line...
-
May 3rd, 2004, 02:49 PM
#3
Frenzied Member
What you posted should work. try:
echo $row[5]; //the value of $row[5]
or:
echo strlen($row[5]); //The length of the value.
Have I helped you? Please Rate my posts. 
-
May 4th, 2004, 08:03 AM
#4
Thread Starter
Frenzied Member
I've tried all sorts of ways and used the strlen function. It is telling me that when there supposedly isn't anything there, the length of the string is 6 before and after the trim. Now I'm just really confused.
-
May 4th, 2004, 08:36 AM
#5
Originally posted by ober5861
I've tried all sorts of ways and used the strlen function. It is telling me that when there supposedly isn't anything there, the length of the string is 6 before and after the trim. Now I'm just really confused.
Does the data returned correlate with whats in the table?
I'd imagain you have made an error in retrieving the data from the database. Check your query and that the column you are expecting to see corresponds to the column in your sql query.
-
May 4th, 2004, 09:49 AM
#6
Thread Starter
Frenzied Member
It does correlate. The field is a varchar of length 5.... and besides, it doesn't matter what it is returning... it's showing a single space both before and after the trim.
-
May 4th, 2004, 10:56 AM
#7
Frenzied Member
It only trims spaces at start and end of string. Here's of an example at work:
PHP Code:
<?
$a = array();
$a[0] = " a b c ";
$a[1] = " d e f ";
$a[2] = " g h i ";
echo strlen($a[2])."<br>";
echo strlen(trim($a[2]));
?>
this shows you that the lenghts are different.
If you get rid of the strlen() bit you'll see that they output the same anyway, but the source will look different.
Have I helped you? Please Rate my posts. 
-
May 4th, 2004, 11:01 AM
#8
Thread Starter
Frenzied Member
Right... I understand that. But here is what is happening: I'm printing out the raw value with a dash in front and behind. It says that length is 6 using strlen();
I then do a trim on what appears to be one space while strlen() says it's 6 and it outputs the SAME THING: Do you see my frustration?
-
May 4th, 2004, 11:04 AM
#9
Frenzied Member
Oh I totally see your frustration.
try this and see what HTML the server outputs:
PHP Code:
echo "<p>".$row[5]."</p>";
That'll tell you EXACTLY what row[5] is.
Have I helped you? Please Rate my posts. 
-
May 4th, 2004, 11:32 AM
#10
Thread Starter
Frenzied Member
No change, still gives me a space.
-
May 4th, 2004, 11:39 AM
#11
Frenzied Member
Originally posted by ober5861
No change, still gives me a space.
can you post the HTMl it gives? is it just:
"<p> </p>"?
and strlen($row[5]); is still 6? Then something VERY strange is going on.
Have I helped you? Please Rate my posts. 
-
May 4th, 2004, 11:59 AM
#12
Take a look at your HTML source, I think you may be trying to display a field with just spaces. If that is the case the browser will only display one space.
To see all six you'll need to convert the spaces to their HTML Entitiy equivilent: &nbsp;
Try this:
PHP Code:
echo ('[value]' . htmlentities (trim ($row[5])) . ' [value]');
-
May 5th, 2004, 07:39 AM
#13
Thread Starter
Frenzied Member
Acidic, that outputs "<p> </p>".
VisualAd, that outputs [value] [value].
More ideas?
-
May 5th, 2004, 07:55 AM
#14
Frenzied Member
if that is outputting:
Which you said, and also that:
PHP Code:
echo strlen($row[5]);
outputs 6. Which you also said. Then something damn wierd is going on. Are you sure $row is actually an array, try:
PHP Code:
echo count($row);
That should tell you how many elements there are in the array. Also try:
Just to see what it outputs.
Have I helped you? Please Rate my posts. 
-
May 5th, 2004, 08:16 AM
#15
It is apparent that the string does not contain any white space. Try the code below and it will tell you the ascii character code of each character in your string.
Maybe it will shed some light on your problem:
PHP Code:
for ($count = 0; $count < str_len($row[5]); $count++)
echo (ord (sub_str ($row[5], $count, 1)) . '<br />');
-
May 5th, 2004, 08:18 AM
#16
Thread Starter
Frenzied Member
I'm quite sure it is an array as I am able to output the rest of the elements ($row[0], $row[1], $row[2], etc.) just fine. It's like the browser won't display a blank field correctly or allow me to detect a blank field.
-
May 5th, 2004, 08:21 AM
#17
Thread Starter
Frenzied Member
Ok... a light bulb just popped over my head and I have solved the problem... I've detected the content of $row[5] by saying:
PHP Code:
if($row[5] = " ")
and that appears to work as I need it to.
Thanks for all your efforts.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|