簡體   English   中英

WordPress使用正則表達式從帖子中提取圖像和視頻簡碼

[英]Wordpress pull images and video shortcodes from post using regex

希望有人可以幫助我。 我已經在使用正則表達式從wordpress帖子中提取圖像。 雖然不知道如何拉視頻和圖像。 對不起,我在正則表達式上很爛。 這是我正在使用的代碼:

 <?php
    $PostContent = $post->post_content;
    $SearchPattern = '~<img [^\>]*\ />~';

// Run preg_match_all to grab all the images and save the results in $aPics
    preg_match_all( $SearchPattern, $PostContent, $allPics );

// Check to see if we have at least 1 image
    $iNumberOfPics = count($allPics[0]);

    if ( $iNumberOfPics > 0 ) {
        // Now here you would do whatever you need to do with the images
        // For this example the images are just displayed
        for ( $i=0; $i < $iNumberOfPics ; $i++ ) {
            echo '<li>' . $allPics[0][$i] . '</li>';
        };
    };
?>

任何想法如何編輯搜索模式以從帖子內容中提取wordpress視頻嵌入,如下所示:

[video width="640" height="360" m4v="http://localhost/~TandySean/LOH/WP38/wp-content/uploads/2013/11/museum3shot_v1_iphone.m4v"]

感謝任何建議。 -肖恩

第一個正則表達式與html img元素匹配。 因此,您想匹配包含視頻信息的簡碼。

以下正則表達式符合我的要求,並捕獲\\1\\2\\3組中的width,height和m4v屬性

~(?<=\[video)\s+width="([^"]+)"\s+height="([^"]+)"\s+m4v="([^"]+)"]~

EXPLAINED

(?<=\\[video)\\s+ -按字面值匹配此字符串以及其后的一個或多個空格

width=" -從字面上匹配此字符串

([^“] +)” \\ s +-捕獲所有不是雙引號的內容,然后匹配雙引號和一個或多個空格

height=" -實際匹配此字符串

([^“] +)” \\ s +-捕獲所有不是雙引號的內容,然后匹配雙引號和一個或多個空格

m4v=" -從字面上捕獲此字符串

([^"]+)"] -捕獲所有不是雙引號的內容,然后匹配雙引號和右方括號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM