Question

I want to remove some text from my title (using wordpress). Example: Alexandra Stan - Mr. Saxobeat Output: Mr. Saxobeat

I tried many codes, one of this work perfect:

$str = "this comes before – this comes after";
$char =  " - "; 
$strpos = strpos($str, $char); 
$str = substr($str, $strpos+strlen($char)); 
echo $str; 

but after many times of trying and get upsed... i see that in my wordpress article page, when i type "-" in title, wordpress change it automatically with: "–" who is different (bigger) than normal one "-" (copy in another font and you will see the difference).

I try replace "-" with "–" but output is " s comes before - this comes after "

Thanks!

Was it helpful?

Solution

That's an em dash you're trying to replace. However, you're looking for a regular dash. Try running the string through this mess of code first and reading the blog article I got it from

EDIT

A complete working example, basically pasting the example code from the blog article and fixing a small mistake with your substr

function scrub_bogus_chars(&$text) {
    // First, replace UTF-8 characters.
    $text = str_replace(
    array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93",      "\xe2\x80\x94", "\xe2\x80\xa6"),
    array("'", "'", '"', '"', '-', '--', '...'),
    $text);

    // Next, replace their Windows-1252 equivalents.
    $text = str_replace(
    array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
    array("'", "'", '"', '"', '-', '--', '...'),
    $text);
}

// Original string (with em dash)
$text = "this comes before – this comes after";

// Ensure regular dashes will be available
scrub_bogus_chars($text);

// Lastly, extract the interesting part of the original string
$char   =  ' - ';
$strpos = strpos($text, $char); 
$text   = substr($text, $strpos + strlen($char)); 
echo $text . PHP_EOL; 

OTHER TIPS

You should use explode :

$str = "Alexandra Stan - Mr. Saxobeat ";
$char =  " - ";
$str = explode($char, $str);
echo $str[1];

Returns

Mr. Saxobeat

It is not a normal dash - it is a special utf8 char . You need to use the special char in your code. With explode() :

$parts = explode(' – ', 'this comes before – this comes after');
echo $parts[1];

Or with preg_match():

preg_match('~\– (.*)~', 'this comes before – this comes after', $matches);
echo $matches[1];

Here:

echo explode($char, $str)[1];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top