Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird dash character in PHP

Tags:

php

I have a weird dash in my text, which isn't being detected in a str_replace.

Here is an example:

Sun: 10:00 – 3:00pm

I don't know if the dash will show up on here.. but when it is inserted into my table, it is like a square box with the characters 0096

It looks a lot like an – - when viewing the source, there is no special characters, just the dash.

str_replace('–', '', $var);

The above replace doesn't seem to catch it, has anyone else had this trouble before?

like image 865
pdlol Avatar asked Jan 27 '12 00:01

pdlol


People also ask

Is a dash special character?

The hyphen is mostly a normal character in regular expressions. You do not need to escape the hyphen outside of a character class; it has no special meaning.

What is this character â?

Â, â (a-circumflex) is a letter of the Inari Sami, Skolt Sami, Romanian, and Vietnamese alphabets. This letter also appears in French, Friulian, Frisian, Portuguese, Turkish, Walloon, and Welsh languages as a variant of the letter "a".

How to avoid special characters in PHP?

Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).

How to remove a character PHP?

The trim() function in PHP removes whitespace or any other predefined character from both the left and right sides of a string. ltrim() and rtrim() are used to remove these whitespaces or other characters from the left and right sides of the string.


1 Answers

That's an en dash. In php, the most portable way to get it is with html_entity_decode:

$endash = html_entity_decode('–', ENT_COMPAT, 'UTF-8');
echo str_replace($endash, '(en dash)', 'Sun: 10:00 – 3:00pm');

Note that this only works if your website encoding is UTF-8 and your editor encoding(or the encoding of the third argument to str_replace) is as well. If you use another encoding (and you should use the same both for website and editor), replace the third parameter of html_entity_decode with its name.

like image 139
phihag Avatar answered Sep 22 '22 21:09

phihag