2017-03-07 84 views
0

我想用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,但是,我不能让它删除破折号..

任何帮助,将不胜感激!

+3

见的差异,这种( - )是从我的键盘和这个破折号( - )将复制您的变更 '$标题问题 – Shan

+0

粘贴= str_replace函数( ' - ', '',$标题);' 到'$ title = str_replace(' - ','',$ title);' 不幸的是什么也没发生 – Matthias

+0

检查此:https://eval.in/749792 – Shan

回答

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

它为我工作。

0

- 短破折号

- 破折号

也许你正试图取代什么是其中之一?

您可以通过将'alt'与'0150'用于 - 和用'0151'来重现它们。

我希望这会有所帮助。

+0

OP已经有他的代码中的两个。 – Mike