Getting youtube links from post_content not working

admin2025-06-04  3

I am trying to retrieve youtube links from post_content but it does not work..

function get_yts() {
      $query_args = array(
        's' => 'youtube/watch?v=',
      );

      $posts = get_posts( $query_args );
      foreach ($posts as $p) {
        $matches = array();
        preg_match('|\?v=([a-zA-Z0-9_]+)|', $p->post_content, $matches);
        echo '$xxmatches: ' . $matches . ' -> '. $p->post_content;
      }
    }

result show be an empty array:

$xxmatches: Array 

what am I doing wrong ?

EDIT:

found better regex which matches really in some editor but not in here:

<?php
        //Enter your code here, enjoy!
$text = '<!-- wp:core-embed/youtube {"url":"","type":"video","providerNameSlug":"youtube","className":"wp-embed-aspect-4-3 wp-has-aspect-ratio"} -->
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">

</div></figure>';

$matches = array();
preg_match('/http(?:s?):\/\/(?:www\.)?youtu(?:be\\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?‌​=]*)?/', $text, $text);
echo '$xxmatches: ' . sizeof($matches);

shows me 0 size..

I am trying to retrieve youtube links from post_content but it does not work..

function get_yts() {
      $query_args = array(
        's' => 'youtube/watch?v=',
      );

      $posts = get_posts( $query_args );
      foreach ($posts as $p) {
        $matches = array();
        preg_match('|http://www.youtube/watch\?v=([a-zA-Z0-9_]+)|', $p->post_content, $matches);
        echo '$xxmatches: ' . $matches . ' -> '. $p->post_content;
      }
    }

result show be an empty array:

$xxmatches: Array 

what am I doing wrong ?

EDIT:

found better regex which matches really in some editor but not in here:

<?php
        //Enter your code here, enjoy!
$text = '<!-- wp:core-embed/youtube {"url":"https://www.youtube/watch?v=ycsODGR5IDQ","type":"video","providerNameSlug":"youtube","className":"wp-embed-aspect-4-3 wp-has-aspect-ratio"} -->
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
https://www.youtube/watch?v=ycsODGR5IDQ
</div></figure>';

$matches = array();
preg_match('/http(?:s?):\/\/(?:www\.)?youtu(?:be\\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?‌​=]*)?/', $text, $text);
echo '$xxmatches: ' . sizeof($matches);

shows me 0 size..

Share Improve this question edited Jan 24, 2019 at 15:51 FrancMo asked Jan 24, 2019 at 15:24 FrancMoFrancMo 1537 bronze badges 5
  • Are you sure that your search is returning results? What happens when you type $posts = get_posts( $query_args ); var_dump($posts); exit;? youtube/watch?v= contains a couple of special characters namely /, ?, & = which could be filtered out or escaped when the search terms are searched for by WordPress. – admcfajn Commented Jan 24, 2019 at 15:42
  • you can't echo an array. echo '$xxmatches: ' . implode(', ', $matches) . ' -> '. $p->post_content; – mrben522 Commented Jan 24, 2019 at 15:43
  • Also you need to escape special characters in your regex https?:\/\/www\.youtube\\/watch\?v=([a-zA-Z0-9_]+) – mrben522 Commented Jan 24, 2019 at 15:47
  • This code also won’t match any https YouTube embeds, which I believe is standard these days. – Jacob Peattie Commented Jan 24, 2019 at 15:49
  • @jacob just added the s? to that string – mrben522 Commented Jan 24, 2019 at 15:50
Add a comment  | 

1 Answer 1

Reset to default 1

Ok here is working code:

function get_yts() {
  $query_args = array(
    's' => 'youtube/watch?v=',
  );

  $posts = get_posts( $query_args );
  foreach ($posts as $p) {
    $input_line = $p->post_content;
    $output_array = array();
    preg_match('/http(?:s?):\/\/(?:www\.)?youtu(?:be\\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?‌​=]*)?/', $input_line, $output_array);
    print_r($output_array);
  }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748977683a315326.html

最新回复(0)