user3314053 user3314053 -3 years ago 250
PHP Question

php Remove em dash from string

This should be simple.

I've looked at other solutions here:Remove a long dash from a string in JavaScript?

and here:
Weird dash character in PHP

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>";


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

How do I remove this character? Thank you.

Answer Source

This specifies an array of possible removals,

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

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

echo $s1clean;

When ran,

ALIEN FILM MOVIE PSP Sony Boxed & Complete

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

Edit 1

Reading material:

str_replace

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download