Support

Account

Home Forums Add-ons Repeater Field preg_match inside repeater

Solved

preg_match inside repeater

  • <?php if( have_rows('video') ): ?>
    	<?php while ( have_rows('video') ) : the_row(); ?>
    		<?php
    		$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];

  • 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.