FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Small issue with php switch/case and single quotation





Aredon
I have a small function im using to declare meta keywords and such around my site. I'm having some trouble getting any sections that have a single quotation mark " ' " in their names.

For example the below code will fail to match the condition

Code:

case 'kid\'s-health':
                  $keywords='';
                  $description='';
                  $forum_id='';
               break;


While these match just fine.

Code:

case 'fitness':
                  $keywords='';
                  $description='';
                  $forum_id='';
               break;


I've killed the script with die($condition), and it echos "kid's-health" which by all accounts should match. I've also tried changing the case to be double-quotes, but with no success. I feel like I'm missing something simple, but I'd like some help if its not too much trouble.
jmraker
I would add test code to see what if ($condition == "kid's-health") does

Code:

// If it returns false the ' letter is not in the string
var_dump(strpos($condition, "'"))

if($condition == "kid's-health")
  die('ok');



My first thought was that the ' letter might have been entered as a unicode or that another letter that MS-Word uses or some letter being uppercase. If the ' letter checks out my next guess would be to switch in or out of unicode in firefox to see if the letters in the string change
Aredon
Code:
if($subsection == 'kid\'s health'){
               die('yes '.$subsection.'==kid\'s health');
            }else{
               die('no '.$subsection.'!=kid\'s health');
            }

when visiting the page that calls the function....

Quote:

no kid's-health!=kid's health


this makes me want to stab kittens, quite frankly.
sonam
Your code is OK but PHP for some reason sometimes don't do what we want. Try to change single quotes in double quotes. BTW I think case/switch function is much better then if function if you have more then three if/else queries.

Code:
case "kid's-health":
                  $keywords='';
                  $description='';
                  $forum_id='';
               break;


Sonam
Aredon
changing to all double quotes is the first thing I tried, no dice. Also, the if statement is test code... as should be apparent by the die()'s.
kacsababa
The apostrophe must be a different character in the input then the condition, something like jmraker said.
Echo $subsection out to the browser and compare, maybe they are just lookalike, for example and ' are looks totally the same with some fonts.
Aredon
the die i posted earlier echos both to the browser, the result was copy pasted into a quote.

EDIT: got it figured out after viewing source, one of the characters had been converted to an HTML entity for some reason.

Code:

$subsection = html_entity_decode($subsection,ENT_QUOTES,'UTF-8');
fixed it right up! Thanks for the help everyone.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.