Php Wordpress使用正则表达式从post中提取图像和视频短代码
希望有人能帮我解决这个问题。我已经在用正则表达式从wordpress帖子中提取图像了。但是,我不知道如何同时提取视频和图像。对不起,我的正则表达式很烂。以下是我使用的代码:Php Wordpress使用正则表达式从post中提取图像和视频短代码,php,regex,wordpress,Php,Regex,Wordpress,希望有人能帮我解决这个问题。我已经在用正则表达式从wordpress帖子中提取图像了。但是,我不知道如何同时提取视频和图像。对不起,我的正则表达式很烂。以下是我使用的代码: <?php $PostContent = $post->post_content; $SearchPattern = '~<img [^\>]*\ />~'; // Run preg_match_all to grab all the images and save the
<?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>';
};
};
?>
谢谢你的建议Sean第一个正则表达式与html
img
元素匹配。因此,您希望匹配包含视频信息的短代码
下面的正则表达式与我认为您需要的匹配,并捕获组\1
、\2
和\3
中的宽度、高度和m4v属性
~(?<=\[video)\s+width="([^"]+)"\s+height="([^"]+)"\s+m4v="([^"]+)"]~
~(?哇,谢谢。这比我想象的要近得多。现在我只需要弄清楚如何将它输出到一个可行的视频标签,并将其包含在图像中。感谢您的帮助。
~(?<=\[video)\s+width="([^"]+)"\s+height="([^"]+)"\s+m4v="([^"]+)"]~