regex
  • wordpress
  • preg-replace
  • preg-match
  • 2015-04-05 90 views 0 likes 
    0

    對於正則表達式專家來說,這可能是微不足道的。可悲的是,那不是我。如何在Wordpress風格的簡碼之間提取字符串?

    鑑於以下字符串:文本:

    這裏是一個段落和[標註]這是在系統的測試[/標註]。

    我想使用的preg_match()和/或preg_replace函數來創建以下

    <div class="callout">this is a test</div> 
    

    這裏是現在的我......

    $content = 'Here is a paragraph and [callout]this is a test[/callout] of the system.'; 
    $pattern = "/[callout](.*?)[\/callout]/s"; 
    $matches = array(); 
    preg_match($pattern, $content, $matches); 
    var_dump($match[0]); 
    

    ...我卡住上面的模式似乎包括標籤,即。

    [callout]this is a test[/test] 
    

    ...我錯過了什麼?

    TIA,

    +0

    你檢查過'$ matches [1]'? – d79 2015-04-05 00:17:25

    回答

    1

    嘗試使用此正則表達式,

    \[callout\](.*?)\[\/callout\] 
    

    它將capture the groupthis is a testHere is a paragraph and [callout]this is a test[/callout] of the system

    Working Demo

    1

    你應該逃脫方括號:

    $pattern = "~\[callout\](.*?)\[/callout\]~s"; 
    

    而作爲preg_match基準說:

    如果提供的比賽,則充滿了搜索的結果。 $matches[0]將包含匹配完整模式的文本,$matches[1]將具有匹配第一個捕獲的括號內子模式的文本,依此類推。

    您可以在$matches[1]找到想要的文字。

    相關問題