php
  • replace
  • 2015-10-19 61 views 1 likes 
    1

    之間的文本我想替換字符串中包含的,這就像所有的圖像:用一個CID參考更換關鍵詞

    <img src="data:image/png;base64,IMAGE DATA 
    

    。 由於圖像無法獲得相同的cid,我需要用增量替換它們。但我不知道如何。目前爲止我發現的只是以下代碼,它用相同的代碼替換字符串:

    $string = 'normal text [everythingheregone] after text '; 
    $pattern = '\[.*?]'; 
    $replacement = '[test]' 
    echo preg_replace($pattern, $replacement, $string); 
    //normal text [test] after text 
    

    你有什麼想法嗎?

    回答

    0

    感到羞恥,我應該已經搜查一點點: 這裏是我已經找到了與增量替換:

    $count = 0; 
    preg_replace_callback('/test/', 'rep_count', $content); 
    
    function rep_count($matches) { 
    global $count; 
    return 'test' . $count++; 
    } 
    
    相關問題