Given a URL and the task is to check the URL contains certain string or not. The URL are basically the strings. So in order to check the existence of certain strings, two approaches can be followed. The first approach is used to find the sub string matching in a string and second approach is to find a regular expression match. PHP contains functions for these two approach.
strpos() Function: The strpos() function is used to find the first occurrence of a sub string in a string. If sub string exists then the function returns the starting index of the sub string else returns False if the sub string is not found in the string (URL).
int strpos( $String, $Substring )
Parameters: The strpos() function accepts two parameters as mentioned above and described below.
- $String: This parameter holds the text where searching perform.
- $Substring: This parameter holds the pattern or sub string which is to be searched.
Program: PHP program to find the certain string in an URL using strpos() function.
gfg exists in the URL
function not exists in the URL
Note: The strpos() function finds the sub string in a text using string matching method. Sometime it gives undesired result. For example: if string URL is https://www.geeksforgeeks.org/myfunction and sub string is function then sub string exist in the string URL. Suppose a website wants to display the result of function but it display the result of myfunction which is different. The strpos() function does not check if a sub string present as a whole or it is present with suffix or prefix.
Note: To solve this problem that is to find whether the exact pattern is present in a string(URL) or not preg_match() function is used.
preg_match() Function: The preg_match() function is used to find the exact match of a pattern in a text using regular expression search. Here given a regular expression pattern the function do a search on the text and find the exact match if present. This function returns true if pattern is present and false if the pattern is not present.
preg_match( $pattern, $subject )
Parameters: The preg_match() function accepts two parameters as mentioned above and described below.
- $pattern: It is the regular expression pattern for searching as a string
- $subject: It is the text string upon which the regular expression pattern is searched.
Program 2: PHP program to find exact match of a string in an URL
gfg exist in the URL
function does not exist in the URL
- How to remove portion of a string after a certain character in PHP?
- How to check an element has certain CSS style using jQuery ?
- How to select ID that starts with certain character using jQuery ?
- How to select a div with a certain class using jQuery ?
- How to insert new row at a certain index in a table in jQuery ?
- How to change state continuously after a certain amount of time in React?
- PHP | Check if two arrays contain same elements
- How to check the existence of URL in PHP?
- How to get parameters from a URL string in PHP?
- Program to get the subdomain of a URL using PHP
- Download file from URL using PHP
- How to test a URL for 404 error in PHP?
- PHP | fopen( ) (Function open file or URL)
- Get the full URL in PHP
- Saving an Image from URL in PHP
- How to add http:// if it doesn't exists in the URL in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.