Jump to content
bschultz

Migrating From Wordpress...need help with an mp3 player

Recommended Posts

Posted (edited)

I'm finally moving away from Wordpress.  I've used a couple of different mp3 players in the past, and I need to figure out how to replace a link to an mp3 file or just some text with xxx.mp3 with an html5 audio player.

The one plugin I used replaced all links with a player button...another plugin I used you only needed to have .mp3 at the end, and it would replace the text with a button player.

I have a VERY hard time wrapping my head around regex.  How can I accomplish these two scenarios with 1 regex?
 

<a href=http://domain.com/soundfile1.mp3>Some File</a>

and

http://domain.com/soundfile1.mp3

 


Are both in the old Wordpress database.  The plugin has a button to press to play the file.

Both need to be replaced with this:
 

<audio controls>
<source src="http://domain.com/soundfile1.mp3" type="audio/mpeg"/>
</audio>

 

Edited by bschultz

Share this post


Link to post
Share on other sites

You replaced Wordpress with what?  

I don't know how you get the entry from the DB, or what templating looks like but let's assume you are just using something like pure PHP.  I'll assume that there's a variable named "$soundFile" available with the existing URL.

<?php  
   /// various code
?>
<audio controls>
    <source src="<?= $soundFile ?>" type="audio/mpeg">
</audio>

<?php
    /// more php code if needed

Your type#2 url's that just have the url to the .mp3 will work perfectly.   Only your entries that have encoded the url inside an anchor tag would be a problem. 

What I'd do is fix them in the DB with a SQL statement.  

Tip:  Anytime you do a global UPDATE like this you need to be very careful to test and have a backup.  I usually will make a backup table using something like this:

CREATE TABLE t_atable LIKE atable; 
INSERT t_atable SELECT * FROM atable;

So in this example, I assume your table is named `sound` and the column to have it's data fixed is named mp3_file.

 

UPDATE sound 
   SET mp3_file = 
       SUBSTR(mp3_file, 
              POSITION('http:' IN mp3_file), 
              (POSITION('.mp3' IN mp3_file)+4 - POSITION('http:' IN mp3_file)
       ));

Here is a dbfiddle that proves this will work with versions as old as MySQL 5.5. 

Hopefully you get the idea that it locates the 'http:' and the '.mp3' and uses those positions to carve out a substring with just the URL.  It works fine if there is only the url pre-existing in the column.  It's also impervious to small details like whether or not the url inside an anchor tag src has quotes around it or not.  Assuming you are just putting new url's in the column in the future, you would only need to run this once to clean up your db.

Share this post


Link to post
Share on other sites

I'm replacing Wordpress with my own content management system.  Wordpress is just too bloated for my liking.  The url's for the mp3's are embedded into the stories.

	Our final Beaver Fever Friday of the year was yesterday.  What a show.  It's always nice to get to know these seniors over their careers.  Here are the interviews if you missed them:
	Christa Benson - Track <a href="http:///beaverradionetwork.com/audio/1011/brnpodcasts/BFF2019/ChristaBenson.mp3">Christa Benson - Track</a><br /><br />
	Cody Cook - MGolf <a href="http:///beaverradionetwork.com/audio/1011/brnpodcasts/BFF2019/CodyCook.mp3">Cody Cook - MGolf</a><br /><br />
	



There is NO VARIABLE in the database content.

Here's what I need the code to look like

	Our final Beaver Fever Friday of the year was yesterday.  What a show.  It's always nice to get to know these seniors over their careers.  Here are the interviews if you missed them:
	Christa Benson - Track<audio controls> <source src="http:///beaverradionetwork.com/audio/1011/brnpodcasts/BFF2019/ChristaBenson.mp3"  type="audio/mpeg"> </audio> <br /><br />

Cody Cook - MGolf <audio controls> <source src="http:///beaverradionetwork.com/audio/1011/brnpodcasts/BFF2019/CodyCook.mp3"  type="audio/mpeg"> </audio> <br /><br />
	

Share this post


Link to post
Share on other sites

Have you looked into using PHP's DOMDocument? More information can be found here:
https://www.php.net/manual/en/class.domdocument.php

Basically, you would need to load the HTML code into DOMDocument
https://www.php.net/manual/en/domdocument.loadhtml.php

Then you could use getElementsByTagName() to get the anchor tags
https://www.php.net/manual/en/domdocument.getelementsbytagname.php

Then loop through the anchor tags and use getAttribute() to get the href attribute
https://www.php.net/manual/en/domelement.getattribute.php

Share this post


Link to post
Share on other sites

This should do it.

<?php
$text = <<<T
    Our final Beaver Fever Friday of the year was yesterday.  What a show.  It's always nice to get to know these seniors over their careers.  Here are the interviews if you missed them:
    Christa Benson - Track <a href="http:///beaverradionetwork.com/audio/1011/brnpodcasts/BFF2019/ChristaBenson.mp3">Christa Benson - Track</a><br /><br />
    Cody Cook - MGolf <a href="http:///beaverradionetwork.com/audio/1011/brnpodcasts/BFF2019/CodyCook.mp3">Cody Cook - MGolf</a><br /><br />
T;
$text = preg_replace("/<a.+href=(\"|')([^\"']+)(\"|').+<\/a>/", 
                     "<audio controls> <source src=\"\\2\"  type=\"audio/mpeg\"> </audio>", 
                     $text);
echo $text;

?>


 

Share this post


Link to post
Share on other sites

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.