Selecting specific audio files with regex

Here’s something simple that I hope somebody can help me with.

I have some code that loops (randomly selected) audio, but doesn’t loop audio with a specific file name. I’d like to extend that to a group of files that contain a specific word within the filename, but can’t quite figure it out.

 <?php foreach($page->audio()->shuffle()->limit(1) as $audio): ?>
	<?php if($audio->filename() == 'specificfile.mp3'): ?>
		<audio class="audio" preload="auto" controls="controls" autoplay="autoplay" ><source src="<?php echo $audio->url() ?>" type="<?php echo $audio->mime() ?>" ></audio>
	<?php else: ?>
		<audio class="audio" preload="auto" controls="controls" autoplay="autoplay" loop="loop"><source src="<?php echo $audio->url() ?>" type="<?php echo $audio->mime() ?>" ></audio>
	<?php endif ?>
 <?php endforeach ?>

Have a look at filter() with callback.

I don’t quite see the purpose of the loop considering you want to get one file only?

I did, but without success.

Maybe it doesn’t make that much sense if you only want to check what sort of file you got. So what you probably need is an elseif?

Each page has a number of audio files available, but only one is randomly picked using the shuffle function.

What I would like to achieve is that if that randomly picked file has a specific word in the filename then it isn’t looped. For any other randomly picked file without the keyword in the file name the audio is looped.

<?php 
$keyword = 'specific';
$audio = $page->audio()->shuffle()->first(); 
?>
<audio class="audio" preload="auto" controls="controls" autoplay="autoplay" <?php e(strpos($audio->filename() , $keyword), 'loop="loop"', '') ?>"><source src="<?php echo $audio->url() ?>" type="<?php echo $audio->mime() ?>" ></audio>

Wonderful. Thank you!
I wasn’t aware of that approach.

There was an extra " just before the first audio closing bracket, and I swapped the true and false of the ecco around – now it’s working beautifully.

<audio class="audio" preload="auto" controls="controls" autoplay="autoplay" <?php e(strpos($audio->filename(), $keyword), ' ', 'loop="loop"') ?> ><source src="<?php echo $audio->url() ?>" type="<?php echo $audio->mime() ?>" ></audio>