bschultz Posted August 1, 2019 Share Posted August 1, 2019 (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 August 1, 2019 by bschultz Quote Link to comment https://forums.phpfreaks.com/topic/309054-migrating-from-wordpressneed-help-with-an-mp3-player/ Share on other sites More sharing options...
gizmola Posted August 2, 2019 Share Posted August 2, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309054-migrating-from-wordpressneed-help-with-an-mp3-player/#findComment-1568840 Share on other sites More sharing options...
bschultz Posted August 4, 2019 Author Share Posted August 4, 2019 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 /> Quote Link to comment https://forums.phpfreaks.com/topic/309054-migrating-from-wordpressneed-help-with-an-mp3-player/#findComment-1568858 Share on other sites More sharing options...
cyberRobot Posted August 9, 2019 Share Posted August 9, 2019 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 DOMDocumenthttps://www.php.net/manual/en/domdocument.loadhtml.php Then you could use getElementsByTagName() to get the anchor tagshttps://www.php.net/manual/en/domdocument.getelementsbytagname.php Then loop through the anchor tags and use getAttribute() to get the href attributehttps://www.php.net/manual/en/domelement.getattribute.php Quote Link to comment https://forums.phpfreaks.com/topic/309054-migrating-from-wordpressneed-help-with-an-mp3-player/#findComment-1568911 Share on other sites More sharing options...
Zane Posted October 1, 2019 Share Posted October 1, 2019 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/309054-migrating-from-wordpressneed-help-with-an-mp3-player/#findComment-1570174 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.