How to remove 'em' dash from a string?

user3314053 picture user3314053 · Aug 15, 2015 · Viewed 20k times · Source

I've looked at other solutions here and here, but it's not working for me.

Code

$s1clean = 'ALIEN - FILM - MOVIE – PSP – Sony - Boxed & Complete';
echo $s1clean;
echo "<br><br>";

// Remove dash
$s1clean = str_replace('-', '', $s1clean);

// Remove em dash
$em_dash = html_entity_decode('&#x2013;', ENT_COMPAT, 'UTF-8');
$s1clean = str_replace($em_dash, '', $s1clean);

$em_dash2 = html_entity_decode('&#8212;', ENT_COMPAT, 'UTF-8');
$s1clean = str_replace($em_dash2, '', $s1clean);

$s1clean = str_replace('\u2014', '', $s1clean);

echo $s1clean;
echo "<br><br>";

Output

"ALIEN FILM MOVIE – PSP – Sony Boxed & Complete"

How do I remove this character?

Answer

Script47 picture Script47 · Aug 15, 2015

This specifies an array of possible removals,

$s1clean = 'ALIEN - FILM - MOVIE – PSP – Sony - Boxed & Complete';

$s1clean = str_replace(["-", "–"], '', $s1clean);

echo $s1clean;

When ran,

Ouput

ALIEN FILM MOVIE PSP Sony Boxed & Complete

I simply copied the weird dash and added it with the actual dash possibility and it worked.

Reading Material

str_replace