php str_replace()函数不能在字符串中使用逗号

时间:2014-09-24 14:47:36

标签: php string wordpress str-replace

我试图替换字符串中的文本,但是如果我在文本中添加逗号,则无效。

实施例: 我在$ postinfo_meta var中有下一个字符串: Android,iOS,Blog,Apple

我想删除Blog,结果将是 Android,iOS,Apple

我尝试使用以下方法执行此操作:

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

但这不起作用。如果我把

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

没有逗号,但结果是 Android,iOS,Apple

有什么想法吗?

以下是带有@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;

1 个答案:

答案 0 :(得分:1)

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

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

它工作正常,输出是&#34; Android,iOS,Apple&#34;不是你需要的吗?