Jump to content
New Reality: Ads For Members ×

Multiple Regex on the Same String


bschultz

Recommended Posts

I'm trying to move away from a Wordpress site.  The site used multiple plugins for taking Youtube URL's in a post and embedding a Youtube player.

 

I have that part figured out.

 

The old site also used a plugin to take an mp3 URL and change that to an html5 audio player.

 

That's where I'm having some problems.  I need to strip the

[php] and [/php]

tags from the Wordpress post...and replace them with correct open and close php tags.  I want to remove all LINKS to mp3 files...and put a player in place.  I also want to take all mp3 URL's...and put a player in place.

 

One wrinkle...in some of the Wordpress posts, I have a php include of another file.  In that included file are mp3 links. 

 

The following code somewhat works.  It matches the third PATTERN correctly (mp3 LINK).  The second PATTERN does NOT match (mp3 URL...no a href tags)

 

Are the second and third PATTERNS conflicting?  Can they both match the same thing?  I don't know nearly enough about regex to know.

 

Also, why isn't the second PATTERN matching a URL? 

 

Also, how can I handle the included file...since it doesn't appear to be matching those LINKS (the included file is in the Wordpress post content...do I need to run eval on that post BEFORE running the regex?  If so, how do you store eval results in a variable for further processing?

<?php

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

$replacements = array();
$replacements[] = '<iframe width="640" height="385" src="http://www.youtube.com/embed/\\3" frameborder="0" allowfullscreen></iframe>';
$replacements[] = '<a href="\\0" class="sm2_button">BRN</a>';  
$replacements[] = '<a href="\\1" class="sm2_button">BRN</a>';                                   

$newwithyoutube1 = str_replace("[php]","<?php ",$row['content']);
$newwithyoutube2 = str_replace("[/php]"," ?>",$newwithyoutube1);
$newwithyoutube3 = preg_replace($patterns, $replacements, $newwithyoutube2);

$newwithyoutube4 = str_replace(' <<a', ' <a', $newwithyoutube3); //the third PATTERN is adding an extra < symbol...so remove it

if (strpos($newwithyoutube4 ,'<br')) { eval('?>'.$newwithyoutube4.'<?php '); }
else  { $nlnewphrase = nl2br($newwithyoutube4);   eval('?>' . $nlnewphrase . '<?php ');  }

?>

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/297984-multiple-regex-on-the-same-string/
Share on other sites

Archived

This topic is now archived and is closed to further replies.



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