2014-09-24 117 views
-2

我试图替换字符串内的文本,但如果我在文本中放置逗号,则不起作用。php str_replace()函数不能用字符串内的逗号工作

例子: 我在$ postinfo_meta VAR的下一个字符串:的Android,iOS的,博客,苹果

我要删除博客,那么结果将是的Android,iOS设备,苹果

我试图用这个做:

$postinfo_meta = str_replace("Blog, ", "", $postinfo_meta); 

但是,这是行不通的。如果我把

$postinfo_meta = str_replace("Blog", "", $postinfo_meta); 

没有它的工作原理逗号,但结果是Android,iOS设备,苹果

任何想法?

这里是与@Goleztrol修复的全功能代码(仍然没有工作):

global $themename; 

    $postinfo_meta = ''; 

    if (in_array('author', $postinfo)){ 
     $postinfo_meta .= ' ' . esc_html__('por',$themename) . ' ' . et_get_the_author_posts_link(); 
    } 

    if (in_array('date', $postinfo)) 
     $postinfo_meta .= ' ' . esc_html__('en',$themename) . ' ' . get_the_time($date_format); 

    if (in_array('categories', $postinfo)) 
     $postinfo_meta .= ' ' . esc_html__('en',$themename) . ' ' . get_the_category_list(', '); 

    if (in_array('comments', $postinfo)) 
     $postinfo_meta .= ' | ' . et_get_comments_popup_link($comment_zero, $comment_one, $comment_more); 

    if ('' != $postinfo_meta) $postinfo_meta = __('Publicado',$themename) . ' ' . $postinfo_meta; 

    $items = array_map('trim', explode(',', $postinfo_meta)); 

    // Filter Blog from the array. A callback function like this is very flexible, 
    // and you can even 'use' variables from the outer scope. 
    $items = array_filter($items, function($item){return $item != 'Blog';}); 

    print("<pre>".print_r($items,true)."</pre>"); 

    // Concat the items back together. Add the space again, if you like that. 
    $postinfo_meta = implode($items, ', '); 

    echo $postinfo_meta; 
+0

“博客”或表达实际字符串内容的任何变体如何? – 2014-09-24 14:50:24

+0

该字符串不包含''博客“',所以它是合理的,它不起作用。你也可以尝试替换'Blg'。所以我想知道为什么在你的情况下,正确的版本不起作用,而错误的版本。 – GolezTrol 2014-09-24 14:52:27

+2

这段代码实际上工作 - > http://codepad.viper-7.com/irmGwv – Novocaine 2014-09-24 14:54:51

回答

1

原谅我,但是当我尝试以下:

$postinfo_meta = "Android, iOS, Blog, Apple"; 
$postinfo_meta = str_replace("Blog, ", "", $postinfo_meta); 
echo $postinfo_meta; 

它正常工作,输出是“Android,iOS,Apple”不是你所需要的吗?