Jump to content

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!

Edited by bschultz
Link to comment
Share on other sites

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.