Jump to content

Multiple Regex on one string


Go to solution Solved by requinix,

Recommended Posts

I'm trying to search a string for Youtube url's and mp3 url's...and replace each with a player.

 

Here's the code:

$patterns = array();
$patterns[0] = '#(http://)(www.)(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})#x';
$patterns[1] = '((https?:\/\/)?(\w+?\.)+?(\w+?\/)+\w+?.(mp3|ogg))';

$replacements = array();
$replacements[0] = '<iframe width="640" height="385" src="http://www.youtube.com/embed/\\3" frameborder="0" allowfullscreen></iframe>';
$replacements[1] = '<a id="m5" class="audio {skin:"green", autoPlay:false, addShadow:false,addGradientOverlay:true}" href="\\0">Beaver Radio Network</a>';


$brnpost = 'This is our story content.  Here is the youtube link: <br /><br /> http://www.youtube.com/watch?v=qvwefWgIQhY <br /><br />Listen Here: http://beaverradionetwork.com/audio/1011/brnpodcasts/leonhardt_skaar_2015.mp3';

echo preg_replace($patterns, $replacements, $brnpost);


If I run each Regex individually, it works.  If I join them into the array, it takes part of the mp3 link...and puts it in the Youtube embed.  Like this:

This is our story content.  Here is the youtube link: <br /><br /> <iframe width="640" height="385" src="http://www.youtube.com/embed/leonhardt_s" frameborder="0" allowfullscreen></iframe>kaar_2015.mp3</body>

What am I doing wrong?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/297678-multiple-regex-on-one-string/
Share on other sites

  • Solution

Your first regex is way too permissive.

'#(http://)(www.)(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})#x'
[^/]+/.+/
Regexes are greedy: they will match as much as possible and only backtrack if they encounter a problem matching the rest of the pattern. [^/]+ will match anything up until the next slash (which is the slash in the first "

") then a slash, then any amount of anything which will take it all the way to the end of the line, then backtrack until it finds another slash (which is the last slash in the MP3 URL). Then 11 characters will match "leonhardt_s" and the MP3 regex is left with just "kaar_2015.mp3".

 

The easiest modification is to make sure the regex stops at whitespace too, not just a slash. The first portion will fail because there isn't another slash to find, the second portion won't match, and the third will. However the third is way too permissive as well because of the .*. You can change that to match anything but ?, &, and whitespace just in case.

 

'#(http://)(www.)(?:youtube(?:-nocookie)?\.com/(?:[^/\s]+/.+/|(?:v|e(?:mbed)?)/|[^?&\s]*[?&]v=)|youtu\.be/)([^"&?/ ]{11})#x'

By the way, the HTML in your $replacements[1] is wrong. And there's no point to using the /x flag if you're not using regex comments.

Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.