2014-09-24 99 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」不是你所需要的嗎?

相關問題