1

enter image description here I want to display only the file name without displaying the url. I want to replace the file name instead of 'document '.

 while($fet=mysql_fetch_assoc($sql1))
            { 
             $i=$i+1;
             $next=$fet['f_name'];
             echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>Document'.$i.'</a></h4>';
            }

if I replace the word 'document' to $next it shows full url as http://www.sample/txt/1/sample.doc I need to display sample.doc.

echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>"'.$next.'"</a></h4>';
epascarello
  • 204,599
  • 20
  • 195
  • 236
user3386779
  • 6,883
  • 20
  • 66
  • 134

4 Answers4

3

There are two options.

Assume you have $next=http://www.sample/txt/1/sample.doc

Option 1:

This works if you think http://www.sample/txt/1/ is same to all folders.

ltrim($next,"http://www.sample/txt/1/");

Option 2:

use basename($next)

This will extract the sample.doc

Magesh Kumaar
  • 1,485
  • 2
  • 10
  • 29
1
<?php
    $link = "http://www.sample/txt/1/sample.doc";
    $linkArray = explode('/', $link);

    echo $linkArray[count($linkArray)-1];

?>

Magesh Kumaar also provide good one

<?php

$link = "http://www.sample/txt/1/sample.doc";
echo basename($link);
?>
Faruk Omar
  • 1,173
  • 2
  • 14
  • 22
1

I included $next1 = basename($next);

while($fet=mysql_fetch_assoc($sql1))
        { 
         $i=$i+1;
         $next=$fet['f_name'];
         $next1 = basename($next);
         echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>'.$next1.'</a></h4>';
        }

Its working now.

user3386779
  • 6,883
  • 20
  • 66
  • 134
0
<?php
$url  = "http://www.sample/txt/1/sample.doc";
echo strstr($url,"sample.");

?>

For More Details

Priyank
  • 3,778
  • 3
  • 29
  • 48