-5

I would like to see if the string "profile.php" ends in or the last 4 charcaters equal ".php"

How can I do this with PHP

Thanks!

Salman A
  • 262,204
  • 82
  • 430
  • 521
user2096890
  • 159
  • 1
  • 2
  • 11
  • 3
    [What have you tried?](http://www.whathaveyoutried.com/) This really is easy in PHP. See [ask advice](http://stackoverflow.com/questions/ask-advice), please. – John Conde Mar 04 '13 at 18:30
  • RTLM: http://php.net/pathinfo http://php.net/substr – Marc B Mar 04 '13 at 18:31
  • possible duplicate of [PHP startsWith() and endsWith() functions](http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions) – Salman A Mar 04 '13 at 18:35

2 Answers2

1

Use substr() to get the last 4 characters, then compare it to the string ".php".

if (substr("profile.php", -4) === ".php")
{
    echo "Ends in .php";
}
else
{
    echo "Doesn't end in .php";
}
Rich Adams
  • 26,096
  • 4
  • 39
  • 62
0

You can try PHP's inbuilt function pathinfo.

<?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"; // return php
echo $path_parts['filename'], "\n";
?>
kwelsan
  • 1,229
  • 1
  • 7
  • 18