Jump to content

String problem


boylesg

Recommended Posts

echo "ItemDesc = |".$ItemDesc."|<br>";$Num = strpos($ItemDesc, "1 x / $, ");echo (string)$Num."<br>";echo "*******************************************<br>";$ItemDesc CLEARLY contains "1 x / $, " from the output of the first echo statement.And yet strpos($ItemDesc, "1 x / $, "); is outputing nothing as echo (string)$Num."<br>"; results in a blank line.What the ###### am I missing?This is bull######!If I change strpos($ItemDesc, "1 x / $, "); to strpos($ItemDesc, " / $, "); I get 4 as the output.But as soon as I change it strpos($ItemDesc, "x / $, "); the output becomes blank.1) If the function strpos is returning FALSE then why isn't something....anything output by echo.2) If function strpos is returning FALSE then why? What is it about 'x' in this string that is causing it to fail to find the sub-string "x / $, ".

Link to comment
Share on other sites

FALSE echoes as an empty string. TRUE echoes as a 1.Have you tried View Source to see what is actually being echoed? You wouldn't notice extra spaces or line breaks if the only text you looked at is in the browser window. You would see them in the source.Should the correct value returned by strpos($ItemDesc, "x / $, ") be 0? That might mess things up.

Link to comment
Share on other sites

First, you never have to cast something to a string to print it. echo and print only output strings, so they are smart enough to do the cast, you don't need to do that yourself.Secondly, this will give you a lot more information:var_dump($Num);That will tell you both the exact value and also the data type of the variable. If it's boolean false, that will tell you instead of printing an empty string, which is what happens when you cast boolean false to a string.Thirdly, since double quotes will do variable substitution, and since you don't need that, use single quotes:$Num = strpos($ItemDesc, '1 x / $, ');That way you know the $ is a literal $. You could also explicitly escape it:$Num = strpos($ItemDesc, "1 x / \$, ");

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...