$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Getting youtube links from post_content not working|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Getting youtube links from post_content not working

matteradmin11PV0评论

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);
  }
}
Post a comment

comment list (0)

  1. No comments so far