2015-12-15 40 views
0

我有一個WordPress短代碼,以[pullquote]打開,以[/pullquote]結尾。我試圖得到開放和結束標籤中的任何內容。正則表達式從WordPress短代碼中提取內容

我是新來的正則表達式,所以我盯着一個簡單的捕獲字母,數字和空格。

\[pullquote\]([0-9a-zA-z\s]*)\[\/pullquote\]

這工作正常,但不佔標點符號等,所以我嘗試(.*)這是做太多,不夠近具體。

最後我想這

\[pullquote\](^(?:\[\/pullquote\])*)\[\/pullquote\]

我不是這裏的術語清楚,但基本上是想獲得任何與[pullquote]捕獲任何來到之後提供它不是[/pullquote]開始,以[/pullquote]結束。

至少在regexr.com它沒有工作,但我認爲這意味着我做錯了什麼。在regexr

[pullquote]Something[/pullquote] 
[pullquote]Something else.[/pullquote] 

我怎樣才能使這項工作,我是做其他事情錯在這裏使用

文本。

感謝

+1

使用'(。*?)'使它成爲一個非貪婪的捕獲。它會在第一時間停下來,而不是捕捉所有的東西 –

+0

不確定這是使用正則表達式而不是簡單的字符串搜索(即'strpos()')最好解決的。 –

+0

@MikeBrant不知道我會如何使用'strpost'來解決這個問題。似乎只得到一個字符串的第一個或最後一個出現,但我可能在一個帖子中有幾個。 –

回答

1

你只需要這樣:

(\[pullquote\])(.+)(\[\/pullquote\]) 

並獲得唯一的是第2組$2

見這裏:https://regex101.com/r/dS8eZ0/2

的信息拉出鏈接:

MATCH INFORMATION 
"(\[pullquote\])(.+)(\[\/pullquote\])/g" 
    1st Capturing group "(\[pullquote\])" 
     "\[" matches the character [ literally   
     "pullquote" matches the characters pullquote literally (case sensitive) 
     "\]" matches the character ] literally 
    2nd Capturing group "(.+)" 
    ".+" matches any character (except newline) 
     "Quantifier: +" Between one and unlimited times, as many times as possible, 
         giving back as needed [greedy] 
    3rd Capturing group "(\[\/pullquote\])" 
     "\[" matches the character [ literally 
     "\/" matches the character/literally 
     "pullquote" matches the characters pullquote literally (case sensitive) 
     "\]" matches the character ] literally 
    "g" modifier: global. All matches (don't return on first match) 
+0

如何添加新行?因爲它是帖子正文並且通常會包含新的行。 '(\ N +)'? –

+0

@jeanpier_re添加'gm'修飾符 –

1

這裏是使用strpos()進行基本搜索,你可能會因爲效果比較而嘗試類似的東西。

function extract_shortcode_content($needle, $haystack) { 
    if(empty($needle) || empty($haystack || !is_string($needle) || !is_string($haystack)) { 
     throw new Exception('Bad input'); 
    } 
    // $needle is just intended to be shortcode value (i.e. 'pullquote') 
    // we will build appropriate start and end tags 
    $needle_trimmed = trim(trim($needle), '[]'); 
    $start_code = '[' . $needle_trimmed. ']'; 
    $end_code = '[/' . $needle_trimmed . ']'; 
    $start_code_length = strlen($start_code); 
    $end_code_length = strlen($end_code); 
    $haystack_length = strlen($haystack); 
    $last_searchable_position = $haystack_length - $start_code_length - $end_code_length - 1; 

    $return_array = array(); 

    // iterate through haystack extracting content 
    $search_offset = 0; 
    $continue = true; 

    while($search_offset < $last_searchable_position) { 
     $start_code_found = strpos($haystack, $start_code, $search_offset) {   
     if ($start_code_found === false) { 
      // no match in remainder of string 
      return $return_array; 
     } 

     // extract content 
     $content_start_position = $code_found + $start_code_length; 
     $end_code found = strpos($haystack, $start_code, $content_start_position); 
     if ($end_code_found === false) { 
      // we couldn't find close for current shortcode open tag. 
      // we don't count this as a match, so let's just return matches we have 
      return $return_array; 
     } 
     $match_length = $end_close_found - $content_start_position; 
     // add content to result array 
     $result_array[] = substr($haystack, $content_start_position, $match_length); 
     // set new search offset position for next iteration 
     $search_offset = $end_code_found + $end_code_length; 
    } 

    return $return_array; 
} 

現在,我不是說你應該使用這個而不是正則表達式的方法。當然,正則表達式方法可以在幾行代碼中得到相同的結果。我只是建議這種方法可能比正則表達式更適合這種用例。這可能是您的用例的微型優化,不值得額外的代碼複雜性。

我只是想提供一個替代的建議正則表達式。

相關問題