How do I split a WordPress title at the – in PHP?

I am working on my WordPress blog and its required to get the title of a post and split it at the “-“. Thing is, its not working, because in the source its &ndash and when I look at the result on the website, its a “long minus” (–). Copying and pasting this long minus into some editor makes it a normal minus (-). I cant split at “-” nor at &ndash, but somehow it must be possible. When I created the article, I just typed “-” (minus), but somewhere it gets converted to – automatically.

Any ideas?

Read More

Thanks!

Related posts

Leave a Reply

2 comments

  1. I think I found it. I remember that I have meet the similar problem that when I paste code in my post the quote mark transform to an em-quad one when display to readers.

    I found that is in /wp-include/formatting.php line 56 (wordpress ver 3.3.1), it defined some characters need to replace

    $static_characters = array_merge( array('---', ' -- ', '--', ' - ', 'xn–', '...', '``', '''', ' (tm)'), $cockney );
    $static_replacements = array_merge( array($em_dash, ' ' . $em_dash . ' ', $en_dash, ' ' . $en_dash . ' ', 'xn--', '…', $opening_quote, $closing_quote, ' ™'), $cockneyreplace );
    

    and in line 85 it make an replacement

    // This is not a tag, nor is the texturization disabled static strings
    $curl = str_replace($static_characters, $static_replacements, $curl);
    
  2. If you want to split a string at the “-” character, basically you must replace “-” with a space.

    Try this:

    $string_to_be_stripped = "my-word-test";
    $chars = array('-');
    $new_string = str_replace($chars, ' ', $string_to_be_stripped);
    echo $new_string;
    

    These lines splits the string at the “-“. For example, if you have my-word-test, it will echo “my word test”. I hope it helps.

    For more information about the str_replace function click here.

    If you want to do this in a WordPress style, try using filters. I suggest placing these lines in your functions.php file:

    add_filter('the_title', function($title) { 
    
    $string_to_be_stripped = $title;
    $chars = array('-');
    $new_string = str_replace($chars, ' ', $string_to_be_stripped);
    return $new_string;
    
    }) 
    

    Now, everytime you use the_title in a loop, the title will be escaped.