views:

61

answers:

3

I'm trying to remove this character, and this character only from a $string with php. I've tried str_replace("»","",$test) and str_replace(chr(187),"",$test) but they can't touch it. The issue is it isn't in the same spot every time, so I can't even get creative with trimming the ends

Any help?

+4  A: 

Are you forgetting that str_replace(old, new, string) doesn't modify the original string, but rather returns a copy of the modified string?

So:

$string = "This is the » character";
$new_string = str_replace("»", "_", $string);
echo $new_string;

Should work (it does for me)!

mjschultz
now that code does work by its self but for some reason won't strip that char from the string... a mystery
Shastapete
I'm a little confused as to how it works by it self, but doesn't strip the character from the string. I assume you have your string in place of the included string and that is what causes it to fail. Perhaps there is more to the string than the eye sees?
mjschultz
There was some hidden chr(194) up next to it. I finally killed it with $title2 = str_replace(chr(194).chr(187), "", $title);
Shastapete
+1  A: 

Wanted to point out that "»" in HTML equals » which is a standard. So, my advise would be that you better use standard characters.

Reference of characters: http://www.w3schools.com/tags/ref_entities.asp

Tom
`»` is a perfectly valid character to use in HTML, no need to escape it. Unless you're using a character set that can't encode it.
deceze
but it will give you a warning when validating, why'd you want that? plus, why you have to encode or decode it? Just wherever you need that character use `»` instead of (alt+0187)..
Tom
What are you talking about? The W3C validator perfectly accepts a `»` in HTML.
deceze
I'm pulling a list of webpage titles from an sql sever and some are formatted in the way Blog » title, but for some reason I can't touch the »
Shastapete
@deceze: Sorry, my bad there. After seeing that "` too, thinking that every would return warning. So yeah, after using every character like that, I've never run into validations errors again (like to keep my code tidy).
Tom
As to what the problem is with HTML encoding it, it litters your code with unreadable blobs. Für German text for example, it's quite ärgerlich to have HTML encoded characters all over your text. Unicode (or other appropriate character sets) thankfully got rid of the need to do that.
deceze
A: 

I ended up stepping through the string one character at a time with

echo ord($test[n]

until I finally found the offending hidden character that was ruining my night.

Thanks for all the help everyone!

Shastapete