将if语句与正则表达式结合使用

时间:2013-12-19 14:27:11

标签: php regex wordpress-theming

当前代码,搜索第一个image和第一个iframe。但是,我希望它只显示它找到的第一个,而不是两者中的第一个。如果首先找到iframe,请显示。如果是image,请显示。

function first_item() {
  global $post, $posts;
  $first_item = '';
  ob_start();
  ob_end_clean();

  if ( preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)) {
      $first_item = $matches [1] [0];
 echo "<img src=" . $first_item . ">";

}  if (preg_match_all('/<iframe.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)) { 

      $first_item = $matches [1] [0];

  echo "<iframe src=" . "'" . $first_item  . "'" . "/"  .">" . "</iframe>";

 } }

任何帮助都会很棒。

4 个答案:

答案 0 :(得分:0)

由于两者都使用src属性,因此您可以使用快捷方式:

'/<(img|iframe).+src=...............................>/i'

然而,请注意小马在看......

答案 1 :(得分:0)

无需使用preg_match_all,您只需要preg_match进行一次匹配:

$pattern = <<<'LOD'
~
## begining of the tag ##
<i (mg|frame) \b  # capture group 1: to know which tag name it is

## all possible content before the src attribute ##
(?>
    [^s>]++         # all that is not a "s" or a ">" (to fail at the tag end) 
  |
    \B s++          # "s" without a word boundary can't be the "s" from "src" 
  |
    s (?!rc \s*+ =) # "s" not followed by "rc="
)++                 # repeat the group one or more times
                    # (there is at least a space)

## from the attribute name until the begining of the attribute value ##
src \s*+ = \s*+ ["']?+  # spaces and quote are optional

## attribute value ##
( [^\s"'>]++ ) # capture group 2: url
               # the value can't contain spaces, quotes, or >, since it is
               # an URL
~ix
LOD;

if (preg_match($pattern, $post->post_content, $match)) {
    $first_item = $match[2];
    if ($match[1]=='mg') 
        echo '<img src="' . $first_item . '"/>';
    else
        echo '<iframe src="' . $first_item . '"></iframe>';
}    

答案 2 :(得分:0)

获取两个匹配项,找到首先出现的匹配项,然后显示。

preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $img_matches, PREG_OFFSET_CAPTURE);
preg_match('/<iframe.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $iframe_matches, PREG_OFFSET_CAPTURE);

if ($img_matches[0][1] < $iframe_matches[0][1]) {
  echo "image first";
}
else {
  echo "iframe first";
}

这假设图像和iframe都在内容中。否则,请先检查匹配是否找到任何内容。

答案 3 :(得分:0)

我创建了一个示例,我希望这会有用:

function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      $iframe = '';
      $link = get_permalink($postID);

      ob_start();
      ob_end_clean();

      $first_img_output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $first_img_matches);
      $first_img = $first_img_matches[1][0];

      $iframe_output = preg_match_all('/<iframe.+src=[\'"]([^\'"]+)[\'"].*><\/iframe>/i', $post->post_content, $iframe_matches);
      $iframe = $iframe_matches[1][0];

      if ( $first_img < $iframe ) {
        echo('<div class="fluid-width-video-wrapper" style="padding-top: 56.25%;"><iframe src="' . $iframe . '" frameborder="0" allowfullscreen></iframe></div>');
      } else {
        echo '<a href="'.$link.'" class="block-thumb">';
        echo('<img src="'.$first_img.'">');
        echo '</a>';
      }
    }