Skip to content
Advertisement

How to remove unwanted characters/text – php

When I retrieve a file, it outputs:

...22nlarray(3) { [0]=> string(62) "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums/." [1]=> string(63) "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums/.." [2]=> string(69) "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums/22nl.mp3" }

I, however, only want the 22nl displayed. I do not want the rest over there. How can I do that? Is there a function that deletes the rest of the output except 22nl (which is a filename)?

My PHP code:

   // get contents of the current directory
    $contents = ftp_nlist($conn_id, $destination_folder);

   foreach ($contents as $mp3_url) { 
     $filename = basename($mp3_url, ".mp3");
     echo "<a href='$mp3_url'>$filename</a>";

 } 
     var_dump($contents);

There are similar questions to mine, however, they did not provide a good answer for me. Greetings,

Rezoo Aftib

Advertisement

Answer

We can say that your value from the array will be

$value='string(69) "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums/22nl.mp3"';

So, you can make that to export your file name:

$value = explode('"',$value);
$exploded = explode('/', $value[1]);
$full_mp3_name = end($exploded);
$just_name = explode(".",$full_mp3_name);
$just_name = $just_name[0];

When you print $full_mp3_name you will need to have 22nl.mp3 When you print $just_name you will need to have 22nl

It will better with functions but its example if it will be option for you.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement