Home Forums Add-ons Repeater Field preg_match inside repeater


preg_match inside repeater

    • irvinph

    • March 6, 2020 at 12:51 pm
    <?php if( have_rows('video') ): ?>
    	<?php while ( have_rows('video') ) : the_row(); ?>
    		$url = get_sub_field('youtube_url');
    		preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
    		<?php echo $youtube_id = $match[1]; ?>
        <?php endwhile; else : ?>
    <?php endif; ?>

    Can someone help me please what am I missing here?

    preg_match works if non repeater…

  • I think it should be something like

    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
      echo $match[1];

    Unless you are just trying to get the id then it should be something like

    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
       $youtube_id = $match[1];

    but you should not be echoing and setting the value at the same time, this will likely just output true no matter what happens in the match echo $youtube_id = $match[1];

    • irvinph

    • March 9, 2020 at 10:28 am

    Massive Thanks John.. it works perfectly. Cheers.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.