w3resource
php Programming Exercies

PHP String Exercises: Extract the file name from the specified string

PHP String: Exercise-5 with Solution

Write a PHP script to extract the file name from the following string.

Sample String : 'www.example.com/public_html/index.php'

Pictorial Presentation:

PHP String Exercises: Extract the file name from the specified string

Sample Solution:

PHP Code:

<?php
$path = 'www.example.com/public_html/index.php';
$file_name = substr(strrchr($path, "/"), 1);
echo $file_name."\n"; // "index.php"
?>

Sample Output:

index.php

Flowchart :

Flowchart: Extract the file name from the specified string

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to convert the value of a PHP variable to string.
Next: Write a PHP script to extract the user name from the specified email ID.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming