Jump to content

Trying to remove a space from a data call -- help?


illusiveone

Recommended Posts

hi guys, this is my first post on phpfreaks so be easy  ;)

 

this is probably a stupid question, but lemme give you some background first.

 

i just became webmaster for a web portal and the previous webmaster make all kinds of grammatical mistakes in his code. adding apostrophes where they shouldnt be, and misspelling just about everything that you couldnt sound out phonetically.

 

ive been going through all the pages and fixing his (small) mistakes. but for some reason either im not seeing this or im too tired to realize what to change here..

 

                                      <?=$data['fname']."'";?>

                                      <?if (substr($data['fname'],-1)!= "s")

                                      {echo "s";}

                                      else {echo "";}

                                      ?>

 

this comes out as <username>' s comments with a space between the apostrophe and the s.

again, very stupid problem, but a problem nontheless. if someone could point out my obvious oversight, i would be eternally grateful  ;D

 

 

Link to comment
Share on other sites

Firstly, welcome to the forums!

 

I can't see anything wrong with that (apart from the general messy code). I can only assume that it is the the element of the array, $data['fname'], that has a space on the end of it.

 

You could test that by comparing the length of the string and the string itself.

 

<?php
echo $data['fname'].' has a length of: '.strlen($data['fname']).'<br />';
?>

 

So, for example, if the first name is Ben, and the length of the string is 4, we know we have a rogue space in there.

 

p.s. Try to remember to use the


tags for your PHP code (and, where possible, full opening tags - we then get syntax hightlighting making it much easier to read)

Link to comment
Share on other sites

thanks for the suggestions, i used the string "checker" to find out how many characters it would return. using my account it returned 4 chars (my name is alan), which is correct so basically the space is somewhere after the variable. i cant seem to find that particular space in the code that was used.

 

???

Link to comment
Share on other sites

Ah, i got it. Change your code to:

 

<?php
echo $data['fname']."'";
if (substr($data['fname'],-1)!= "s"){
echo "s";
}
?>

 

The space being generated wasn't being echoed by PHP, it was in the HTML. That happened because there was a space between closing the first bit of PHP and opening the second.

Link to comment
Share on other sites

hahahaha thanks i was gona reply and say that i figured it out too...

 

<?php=$data['fname']?>
<?if (substr($data['fname'],-1)!= "s")
{echo "'s";} 
else {echo"";}
?>

 

a little different, but the same end result. not the most beautiful thing out there, but it works  ;D

 

thanks for all your help ginger, if youre ever in louisiana for mardi gras i owe you a beer!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.