• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Tutsplanet

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Twitter Trends
  • Tools
  • About Us

How to check if a string ends with another string in PHP

Sep 22, 2021

This snippet will enable you to check if a string ends with a certain string. Particularly very handy when checking filetypes, like is something a jpg or not.
The third parameter is optional, if set to true then the comparison will be case-specific.

function endsWith($haystack,$needle,$case=true) {
    //If its case specific
    if($case){
      return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
    }
    return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
}

Sample Code

if(endsWith("hello.jpg", '.jpg')) {
    print "true";
}
else {
    print "false";
}
//True

Here strcmp plays major role which compare two strings and returns < 0 if string1 is less than string2; > 0 if string1 is greater than string2, and 0 if they are equal.


Related Posts

  • TypeError: PhpMyAdmin\Export::getFilenameAndMimetype(): Argument #5 ($filenameTemplate) must be of type string, null given, called in /libraries/classes/Controllers/Export/ExportController.php on line 397
  • What is the difference between shuffle() and array_rand() in PHP?
  • How to organize email testing in PHP?
  • How to use __invoke() in PHP?
  • How to create and download a Zip file with PHP
  • Deleting an element from an array in PHP
  • How to Move all zeroes to end of array in PHP with example
  • 5+ Dating Application Script in PHP
  • How to Calculate Whether an Image Is Landscape or Portrait in PHP & Javascript?
  • PHP array_column() Function Example
  • PHP Variable Interpolation
  • Show Thumbnail Image from YouTube Video URL using PHP
  • How to Remove Multiple Elements by Key in PHP Array?
  • Show a number in two decimal places in PHP
  • How to Force Download File from Remote Server in PHP
  • Removing whitespace characters in PHP
  • 7+ Inventory & Stock Management script in PHP
  • How to change PHP version on MAMP – Mac OSX
  • Drag and Drop File Upload using DropzoneJS and PHP
  • Using PHP glob() Function to Find Files in a Directory
  • PHP: Implode () with quotes
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • PHP str_replace() Function
  • How to Copy a File From One Folder to Another With PHP?
  • Ajax Image Upload using PHP and jQuery
  • How to Read Text from Image in PHP
  • MySQL vs MySQLi in PHP
  • A Simple YouTube Video Downloader Script in PHP
  • How to install LAMP with PHP 5.6 on Ubuntu 16.04
  • How to Convert PNG to JPG with compression in PHP?
  • PHP Image resize Example Using Intervention Image Library.
  • How to detect request type in PHP?
  • Install Apache2, MariaDB, And PHP 7.1 (LAMP) On Ubuntu 16.04 LTS Server
  • Create RSS Feed For Website Using PHP
  • How to read command line arguments in PHP
  • Modify PHP Settings In Configuration File in Ubuntu 16.04
  • Get YouTube Video Thumbnail URL using PHP
  • Add Subscribers to List using MailChimp API and PHP CURL
  • Basic PHP File Handling — Create, Open, Read, Write, Append, Close, and Delete
  • 6 Best Free Open Source PHP eCommerce Software Solutions
  • Uploading Files with PHP
  • How to show all PHP errors and warnings?
  • How to import sql file in MySQL database using PHP?
  • How to get current page URL in PHP?
  • How to Export MySQL Table data to CSV in PHP
  • Detect IE5 or IE6 in PHP
  • Count Script Execution Time in PHP
  • Basic Database Connection using PDO in PHP
  • Find Alexa Rank Of a Website Using PHP
  • Create RSS feed for a website in PHP
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
  • Just want to thank us? Buy us a Coffee
  • May be another day? Shop on Amazon using our links.
    Your prices won't change but we get a small commission.

Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Primary Sidebar

Quick Links

  • Top 21 Website Ideas To Make Money Online in 2021
  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Replace “\n” with new line characters, using Notepad++
  • Using Third-Party Libraries in Codeigniter
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • Hierarchical Tree view Category Example in Laravel
  • Laravel Image Intervention Tutorial With Example
  • How to import sql file in MySQL database using PHP?
  • Free VAT Calculator Online

Subscribe

* indicates required

Search Here

Share

   

Hot topics

  • Replace “\n” with new line characters, using Notepad++ 9 views
  • Get Uploads Directory Path in WordPress 8 views
  • A Simple YouTube Video Downloader Script in PHP 7 views
  • All Countries SQL file with Country Codes to Download 7 views
  • Open a URL in a new tab using JavaScript 7 views
  • Post Data to an iframe 6 views
  • Hierarchical Tree view Category Example in Laravel 6 views
  • Laravel db raw and whereRaw() where clause example 6 views
  • How to Get Last Week Data in Laravel using Carbon? 6 views
  • How to Convert PNG to JPG with compression in PHP? 5 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2023 · Planet on Genesis Framework · Powered By BunnyCDN . Network wallpapernoon.com