bschultz Posted August 7, 2015 Share Posted August 7, 2015 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! Quote Link to comment https://forums.phpfreaks.com/topic/297678-multiple-regex-on-one-string/ Share on other sites More sharing options...
Solution requinix Posted August 8, 2015 Solution Share Posted August 8, 2015 (edited) 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 August 8, 2015 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/297678-multiple-regex-on-one-string/#findComment-1518244 Share on other sites More sharing options...
bschultz Posted August 8, 2015 Author Share Posted August 8, 2015 That worked. THANK YOU! You have no idea how much I hate the formatting of regex! Quote Link to comment https://forums.phpfreaks.com/topic/297678-multiple-regex-on-one-string/#findComment-1518253 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.