When you use code like this echo str_replace('- ', '-', '1 – 1'); PHP: you would expect the output to be 1 –1, right? However my output is unaffected by str_replace and I have no clue what the problem is.
try copying this <? echo str_replace('- ', '-', '1 – 1'); # your original line echo '<Br>'.'1 - 1'.'<Br>'; echo str_replace(' – ', '–', '1 – 1'); # hehe my line ?> Code (markup):
yikes ... it should have been echo str_replace('– ', '–', '1 – 1'); Code (markup): not echo str_replace(' – ', '–', '1 – 1'); # hehe my line Code (markup): see any difference
Does it have something to do with character codes or something? Because apparently it works sometimes and sometimes not and I'm about to tear my hair out because I can't figure out why.
yeah ... those symbols look the same to the human eye but they have different ASCII values hmmm im gonna give that problem to my officemates .. see how fast they figure it out ..
I am using the same key for putting that character in the code and for putting it in the form field for which I need this code. Yet it doesn't work.
That's no different to what I wrote initially. Basically I'm trying to convert a title so that it can be displayed in an url and for that I am converting spaces to hyphens. With something like "bla - bla" I would end up with "---" and for some reason neither replacing --- with - nor "- " with - works.
How about simply removing the hyphens first, then creating URLs? $title_tag = "My Three-Sons"; $title_tag = str_replace("-", " ", $title_tag); $title_now = str_replace(" ", "-", $title_tag); echo $title_now; // My-Three-Sons Seems simple enough.
You are right. He is using "–" in the 1 - 1 part. In html view, his code is displayed as @Icheb, try out this instead of your original one and let us know the result. echo str_replace('- ', '-', '1 - 1'); PHP:
Ok, the only way I can get this to work is when I copy & paste the hyphens that the script outputs and then use that in the search rule. It's like it just doesn't recognize the hyphens any other way. It's also not related to the HTML entities, because they would show up in my editor.
I would want to see the character encoding set on the display page and echo the variable before the str_replace and after the replace. This seems too simple to be beating you up.
<?php // works as expected $strg = '1 - 1'; echo $strg; echo '<br />'; $test = str_replace('- ', '-', $strg); echo $test .'<br />'; echo strlen($test); echo '<br /><br />'; // don't work echo str_replace('- ', '-', '1 – 1'); echo '<br />'; echo strlen('1 – 1'); echo '<br />'; echo strlen(str_replace('- ', '-', '1 – 1')); echo '<br /><br />'; // don't work $result = str_replace("- ", "-", "1 – 1"); echo $result . '<br />'; echo strlen("1 – 1"); echo '<br />'; echo strlen($result); echo '<br /><br />'; // let's look at the bad handling, char in quotes echo "char - #45: " . chr(45) . '<br />'; echo "Our Bad char Ascii: " . ord(substr( '1 – 1',2,1)) . '<br />'; echo "Our Bad char text: " .substr( '1 – 1',2,1). '<br />'; echo "char - #150: " . chr(150) . '<br /><br />'; // let's look at the good handling, char in varable after being stored from quotes. echo "char - #45: " . chr(45) . '<br />'; echo "Our good char Ascii: " . ord(substr( $strg,2,1)) . '<br />'; echo "Our good char text: " .substr( $strg,2,1). '<br />'; ?> PHP: Output... 1 - 1 :String to search char(45) 1 -1 :String changed as expected still char(45) 4 :String Length output affected 1 – 1 :Output char(150) 5 :String Length output not affected 1 – 1 :Output char(150) 5 :String Length output not affected char - #45: - :Normal width char(45) Our Bad char Ascii: 150 :Actual char(150) interpruted in string from quotes Our Bad char text: – :Wide dash char(150) displayed char - #150: – :char(150) from text in quotes char - #45: - :Normal char(45) dash Our good char Ascii: 45 :Actual char(45) still interpruted as 45 from variable Our good char text: - : Normal width char(45) from variable Code (markup): Actual test page to see the real results as vB is not showing the result correctly. http://www.cpurigs.com/testcrap/str_rep.php Notice the different width dashes. All I can say is that PHP is handling the the characters differently when stored in variables then when passed to functions.