Ma9ic
Ma9ic

Reputation: 1125

Remove text from string php

I have a table called 'filename'. I try to output <a> tags in a loop from it like this:

<?php

while($sermon = mysql_fetch_assoc($sermonsQ)) {
    echo '<a href="admin/'. $sermon ['filename'] . '">';
    echo 'download</a></td>';
}

Current problem is, that $sermon['filename'] containts a leading path like path/test.mp3. But I need only the filename without the path, like test.mp3. How can I do this?

Upvotes: 1

Views: 126

Answers (3)

Sameer Rangrez
Sameer Rangrez

Reputation: 152

<?php

while($sermon = mysql_fetch_assoc($sermonsQ)) {
    $filename = explode('/',$sermon ['filename']);
    echo '<a href="admin/'. $filename[1] . '">';
    echo 'download</a></td>';
}

Upvotes: 0

Robert
Robert

Reputation: 20286

You can use path_info()

<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>

also mysql_* functions are depracated so you shouldn't use them for example to PDO or mysqli

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 157947

Use basename() for that. It will return the filename without the leading path:

basename($sermon ['filename'])

Upvotes: 5

Related Questions