Forum Moderators: coopster

Message Too Old, No Replies

$String problem

Need help to solve a $string issue

         

Lived

9:50 pm on Feb 13, 2008 (gmt 0)

10+ Year Member



I can't get this to work and have try to find a solution for days now. Now i hope someone here could help me to find a solution on my problem...

-------------------------------------------------------------------
if (($row['height'] == "5\'0\" - 5\'4\"")¦¦($row['height'] == '5 - 5 4'))
{
$height = '1,50 - 1,65';
}
--------------------------------------------------------------------

When i echo $row['height'] it works without any problems and i think i got the problem with qoutes. But why don't it work when i use \" and \' ?

whoisgregg

10:40 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You only escape the character that is wrapping the string. Back slashes in front of the other quote type end up being interpreted as actual slashes, and you probably don't have those in your data which makes the string comparison not work as expected.

$string = 'Don\'t "escape" these doubles '." but you'd escape \"these.\"";
echo $string; // no \ in final output
$string = 'Don\'t \"escape\" everything '." because you\'ll get \"\\\" in your output.";
echo $string; // you end up with \ in output

Lived

10:54 pm on Feb 13, 2008 (gmt 0)

10+ Year Member



5'0" - 5'4" is the string value i got from mysql and this cause the problem

whoisgregg

11:09 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yup, and how you escape the quotes makes a difference in how the == comparison will function.

Running this sample code should do a better job of showing why escaping both quote types is causing a problem in your code.

<?php
echo '<pre>';
$string = <<<___EOF
5'0" - 5'4"
___EOF;
if ($string == "5\'0\" - 5\'4\""){ // escaping all quote types
echo $string.' is equal to '."5\'0\" - 5\'4\"".PHP_EOL;
} else {
echo $string.' is not equal to '."5\'0\" - 5\'4\"".PHP_EOL;
}
if ($string == "5'0\" - 5'4\""){ // wrapped in double quotes, only escapes double quotes
echo $string.' is equal to '."5'0\" - 5'4\"".PHP_EOL;
} else {
echo $string.' is not equal to '."5'0\" - 5'4\"".PHP_EOL;
}
if ($string == '5\'0" - 5\'4"'){ // wrapped in single quotes, only escapes single quotes
echo $string.' is equal to '.'5\'0" - 5\'4"'.PHP_EOL;
} else {
echo $string.' is not equal to '.'5\'0" - 5\'4"'.PHP_EOL;
}
?>

Lived

11:39 pm on Feb 13, 2008 (gmt 0)

10+ Year Member



Great now i got it :) thanx for your time and help

whoisgregg

2:04 pm on Feb 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Happy I could help. :)