str_replace不会删除破折号

时间:2017-03-07 14:14:32

标签: php woocommerce str-replace

我正在尝试使用str_replace来操纵字符串的输出。

function wh_alterProductTitle($title, $id = NULL)
{

    if ('product' == get_post_type($id))
    {

        $title = str_replace('Dames', 'wmns', $title);
    $title = str_replace('dames', 'wmns', $title);
    $title = str_replace('DAMES', 'wmns', $title);
    $title = str_replace('Women’s', 'wmns', $title);
    $title = str_replace('Womens', 'wmns', $title);
    $title = str_replace('Heren', 'mens', $title);
    $title = str_replace('HEREN', 'mens', $title);
    $title = str_replace('heren', 'mens', $title);
    $title = str_replace('-', '', $title);
    $title = str_replace(chr(150), '', $title); // endash
    $title = str_replace(chr(151), '', $title); // endash

       $title = str_replace(['Bijenkorf', 'Zalando', 'NIKE-', 'Adidas', 'Sarenza', 'Filling Pieces', 'Reebok', 'Nike', 'Hardloopschoen', 'New Balance', 'Converse', 'Asics', 'By', 'Beige', 'Sneakers', 'Laag', 'JDsports', 'Trainingschoen', 'Vans', 'Babyschoen', 'Only At JD', 'NIKE', 'Saucony', 'Nubikk'], '', $title);
    }
    return $title;
}

add_filter('the_title', 'wh_alterProductTitle', 10, 2);

此代码更改输出:

Bijenkorf – Adidas Stan Smith W

要:

 – Adidas Stan Smith W

我已尝试str_replace中的破折号的每个实例,但是,我无法删除破折号..

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

str_replace('–', '', $title);

它为我工作。

答案 1 :(得分:0)

- en-dash

- em-dash

也许您要替换的是其中之一?

您可以使用'alt'和'0150'表示 - ,并使用'0151'表示 - 。

我希望这会有所帮助。

相关问题