Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
The code
<?php echo $playlistSong->filename; ?>
reads the path from a database table. In order for the html5 player to play the song it requires the local path that resides in the database table to be converted to an absolute URL path. The revised code converts the backward slash to the forward slash, however, the path for the HTML player is still the local path.

What I have tried:

Below is the revised code that I tried:
 <td>
                      
                          <?php
$url = $playlistSong->filename; 
$url = str_replace(['\\','D:/'], ['/', 'https://m.tamilragam.com/'], $url);
echo $url;
?>
	<div class="mediPlayer">
  <audio class="listen" id="player" preload="none" data-size="50" ontimeupdate="myFunction(this)">
<source src="<?php echo $url; ?>" type="audio/mpeg">
 </audio>

</div>                                                            
 <script>
function myFunction(event) {
    // Trying to stop the player if it goes above 1 second
    if (event.currentTime > 30) {
        event.pause();
        event.currentTime = 0
    }
}
</script>
       </td>

Steps/screenshot -

Path in database: https://m.tamilragam.com/database_details.png[^]
PHP code in the webpage: https://m.tamilragam.com/php_code1.png[^]
Final out put of webpage: https://m.tamilragam.com/test.albums_song.php?album=2000%20Hits[^]
Posted
Updated 17-Nov-23 21:20pm
v6
Comments
Richard Deeming 15-Nov-23 8:18am    
You need to double-check that the filename actually contains the expected value. I've just tried your code in an online PHP compiler, and it worked as expected:
$url = 'D:\Music\2000 Hits\Album.Mp3';
$url = str_replace(['\\','D:/'], ['/', 'https://mywebsite.com/'], $url);
echo $url; // Output: https://mywebsite.com/Music/2000 Hits/Album.Mp3

It appears that the str_replace function sees the sequence \200 and removes it. As the result from your code gives:
https://mywebsite.com/Music0 Hits/Album.Mp3


So using the following code:
PHP
$url = "D:\Music\2000 Hits\Album.Mp3";
$urlb = str_replace("\\", "/", $url);
echo $urlb;

gives the result:
D:/Music0 Hits/Album.Mp3

I cannot find a not in the documentation as to wh this happens, but you probably need to edit the path before doing the replace.
 
Share this answer
 
v3
Comments
Richard Deeming 15-Nov-23 8:15am    
You're testing using a double-quoted literal string. Double-quoted strings[^] will treat \[0-7]{1,3} as an octal character code, so "\200" will be replaced with character 128, which is going to vary depending on the encoding used.

Try using single quotes around the literal instead:
$url = 'D:\Music\2000 Hits\Album.Mp3';
$urlb = str_replace("\\", "/", $url);
echo $urlb; // Output: D:/Music/2000 Hits/Album.Mp3


That shouldn't be an issue for a value read from the database.
Dave Kreskowiak 15-Nov-23 10:24am    
Ah, yes, I remember that. It's one of the reasons why I absolutely hate PHP and will not do any real work in it.
Richard MacCutchan 16-Nov-23 3:18am    
I always forget that, since single and double quotes are treated the same in Python.
Richard has given you a close to perfect solution but it returned the same path to your D: drive as to where you wanted a result of the online url, I have edited it a little which results in the exact output you require. I have hardcoded your path, just edit the '$playlistSong' back to your '$playlistSong->filename' -

PHP
//Your original string using $playlistSong...
$playlistSong = 'D:\Music\2000 Hits\Album.Mp3';

$modifiedString = str_replace('D:\\', 'https://mywebsite.com/', $playlistSong);

//Now replace remaining backslashes with forward slashes...
$modifiedString = str_replace('\\', '/', $modifiedString);

// Output the modified string
echo $modifiedString;


The output will be 'https://mywebsite.com/Music/2000 Hits/Album.Mp3' which you can run as a test in the W3 Editor at - PHP compiler[^]

When you try to replace a backslash, which is an escape character in PHP strings, it will error, you have to use double backslashes - Replace backslashes with PHP str_replace or regex preg_replace[^]
 
Share this answer
 
I had another expert help me to resolve the issue. Below is the code for any future reference in case anyone is stuck like me. Appreciate everyone's help and time to reach a solution.

$url = trim($playlistSong->filename);
$url = str_replace("\\", "/", $url);
$url = str_replace("D:/", "https://mywebsite.com/", $url);
$url = str_replace("d:/", "https://mywebsite.com/", $url);

The HTML player now plays the song after the STR_replace:

https://m.tamilragam.com/albums_song.php?album=2006%20Hits

https://tamilragam.com/artist_song.php?artist=A.%20R.%20Rahman
 
Share this answer
 
Comments
Richard MacCutchan 18-Nov-23 4:19am    
As suggested by Richard Deeming two days ago.
cheekycroak 18-Nov-23 4:32am    
"As suggested by Richard Deeming two days ago" - I do not agree with that response Richard. Please see all of the above response that was provided to me. I am not going to argue and just want to close this query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900