View Full Version : str_replace problem
Icheb
Sep 16th 2006, 9:53 pm
When you use code like this
echo str_replace('- ', '-', '1 – 1');
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.
VONRAT
Sep 16th 2006, 10:01 pm
try retyping ... are those minus signs ?? coz it worked when i tried it
Icheb
Sep 16th 2006, 10:04 pm
It's a hyphen and well, it doesn't work here.
VONRAT
Sep 16th 2006, 10:12 pm
:confused:
try copying this
<?
echo str_replace('- ', '-', '1 – 1'); # your original line
echo '<Br>'.'1 - 1'.'<Br>';
echo str_replace(' – ', '–', '1 – 1'); # hehe my line
?>
Icheb
Sep 16th 2006, 10:19 pm
1 – 1<Br>1 - 1<Br>1–1
That's the output.
VONRAT
Sep 16th 2006, 10:24 pm
yikes ... it should have been
echo str_replace('– ', '–', '1 – 1');
not
echo str_replace(' – ', '–', '1 – 1'); # hehe my line
see any difference :D
Icheb
Sep 16th 2006, 10:30 pm
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.
VONRAT
Sep 16th 2006, 10:39 pm
yeah ... those symbols look the same to the human eye but they have different ASCII values :D
hmmm im gonna give that problem to my officemates .. see how fast they figure it out .. :D
Icheb
Sep 17th 2006, 9:51 am
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.
Icheb
Sep 17th 2006, 12:22 pm
How is it possible that no one knows what's wrong here?
discoverclips
Sep 17th 2006, 12:26 pm
echo preg_replace('/\- /', '-', '1 - 1');
Icheb
Sep 17th 2006, 12:37 pm
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.
GeorgeB.
Sep 17th 2006, 1:00 pm
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.
vishwaa
Sep 17th 2006, 1:02 pm
yeah ... those symbols look the same to the human eye but they have different ASCII values :D
hmmm im gonna give that problem to my officemates .. see how fast they figure it out .. :D
You are right. He is using "–" in the 1 - 1 part. In html view, his code is displayed as str_replace('- ', '-', '1 – 1');
@Icheb, try out this instead of your original one and let us know the result.:D
echo str_replace('- ', '-', '1 - 1');
Icheb
Sep 17th 2006, 3:25 pm
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.
noppid
Sep 17th 2006, 4:23 pm
Have you looked at the variable passed from the form for URL Encoded Chars?
Icheb
Sep 17th 2006, 4:55 pm
It gets saved as normal hyphens in the database, so it's nothing like that either I'm afraid.
noppid
Sep 17th 2006, 5:02 pm
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.
noppid
Sep 17th 2006, 6:06 pm
When you use code like this
echo str_replace('- ', '-', '1 – 1');
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.
<?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 />';
?>
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
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.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.