Php string contains a substring various methods

Sometimes it requires to kiểm tra a particular character or string exists in another string. PHP has many functions to kiểm tra a string contains another substring. Some of these PHP functions are case-sensitive and some are case-insensitive. The uses of two types of functions have been explained in this tutorial with examples.

Bạn đang xem: Php string contains a substring various methods

Pre-requisite:

The scripts used in the examples of this tutorial are written based on the PHP 8 version. Vày the following task before executing the examples of this script.

Install apache2 and PHP 8.Set execute permission for all files & folders under /var/www/html folder where all PHP files will be stored.

Use of strpos() Function

The strpos() function is used khổng lồ find out the position of the first occurrence of the substring in the main string. The syntax of this function is given below.

Xem thêm: Điểm Tên Những Cánh Đồng Hoa Hồng Đẹp Nhất Thế Giới, Ngất Ngây, 10 Cánh Đồng Hoa Đẹp Nhất Thế Giới


strpos(string $main_string, string $search_string, <,int $offset>):int | False

The first argument và the second argument of this function are mandatory arguments & the third argument is optional. The first argument takes the main string value & the second argument takes the search string value. The third argument takes the starting position of the argument. It returns the position value if the substring exists in the main string otherwise returns False.

Example-1: search substring using strpos() function

Create a PHP tệp tin with the following script to search a substring in a string from the starting of the string & the particular position of the string. Here, the tìm kiếm string or substring & the starting position to lớn start searching will be given the URL. The $_GET<> array has been used lớn read the value of the URL query parameters named ‘search’ and ‘pos’.


 //Define a string value$string = "Welcome to lớn obatambeienwasirherbal.com.";//Check tìm kiếm string has given in the URL or notif(isset($_GET<"search">)) $search_str = $_GET<"search">; //Check position value has given in the URL or not if(isset($_GET<"pos">)) $position = $_GET<"pos">; //Find the position of the search string after particular position if (strpos($string, $search_str, $position) !== False) echo "The $search_str found at position ".strpos($string, $search_str, $position); else echo "The string doesn"t contain the string."; //Find the position of the tìm kiếm string elseif (strpos($string, $search_str) !== False) echo "The $search_str found at position ".strpos($string, $search_str);  else //Print message if the position of the search word is not found echo "The string doesn"t contain the string.";else //Print message if no searching string has been given in the URL echo "No searching string found."?>

Output:

The following output đầu ra will appear after executing the above script without any query parameter. Here, the filename is substr1.php that is stored inside /var/www/html/code folder.

http://localhost/code/substr1.php

*
*
*
*
*
*
*
*

Fahmida Yesmin

I am a trainer of website programming courses. I lượt thích to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. Are published: Tutorials4u Help.


View all posts
RELATED LINUX HINT POSTS

Linux Hint LLC, 1309 S Mary Ave Suite 210, Sunnyvale, CA 94087 Privacy Policy and Terms of Use