Uniformly replace dashes in PHP in order to explode

This came about because I was trying to split a title string at the dash within WordPress. Not-so-quickly realizing that I had to account for all the different types of programmatic dashes out there that all look the same visibly. May this code snippet get you across the finish line faster to replace dashes easier.

// Make sure everything is using the same character set.
$title1 = iconv(mb_detect_encoding( get_the_title( $post_ID ), mb_detect_order(), true ), "UTF-8", get_the_title( $post_ID ) );

// Convert the raw string and not HTML encoded output.
$title2 = html_entity_decode( $title1, ENT_NOQUOTES | ENT_HTML5, "UTF-8" );

// Uniformly replace dashes.
$dashes = ['–','–','—','-'];
$title3 = str_replace( $dashes, '-' , $title2 );

$subtitle = explode( '-', $title3 );

Like what you read & want to benefit from my skills?

Hire me!

Leave a Reply

Your email address will not be published. Required fields are marked *