Hello.

It is necessary to determine with $ _SERVER [REQUEST_URI] whether the url of the current page ends in .html

How to make a mask, please tell me?

  • take the last 5 characters via substr and compare with .html ....... or break through the explode with a dot separator and take the last element of the resulting array and compare with html - Alexey Shimansky
  • I'm afraid you should not cut this, better bring the task completely, what should I do? - Naumov

1 answer 1

At least three ways (not counting regulars) comparison with the desired value:

  1. Take the last characters in a string equal to the length of the string being compared:

     $search = 'html'; $str = '/www/htdocs/inc/lib.inc.php'; $lastChars = substr($str, -strlen($search)); if ($search != $lastChars) echo 'Это строка не оканчивается на html'; 

    substr - returns a substring of the string string starting at the specified position and specified. If the starting position is negative, the returned substring starts at the end of the line.

  2. Smash through explode smash the separator "point" and take the last element

     $search = 'html'; $str = '/www/htdocs/inc/lib.inc.php'; $exploded = explode('.', $str); $lastChars = array_pop($exploded); if ($search != $lastChars) echo 'Это строка не оканчивается на html'; 
  3. Take advantage of pathinfo - returns information about the path to the file

     $str = '/www/htdocs/inc/lib.inc.php'; $path_parts = pathinfo($str); echo 'dirname: ' . $path_parts['dirname'], "<br/>"; echo 'basename: ' . $path_parts['basename'], "<br/>"; echo 'extension: ' . $path_parts['extension'], "<br/>"; echo 'filename: ' . $path_parts['filename'], "<br/>"; // начиная с PHP 5.2.0 if ($search != $path_parts['extension']) echo 'Это строка не оканчивается на html'; 
  4. Regulars.

  5. Yes, a lot of different ways, perverted and very perverted.