How to check if URL Contain Certain String using PHP


What is PHP?

PHP (Hypertext Preprocessor) is a popular scripting language designed for web development. It is widely used for creating dynamic and interactive web pages. PHP code can be embedded directly into HTML, allowing developers to mix PHP and HTML seamlessly. PHP can connect to databases, process form data, generate dynamic content, handle file uploads, interact with servers, and perform various server-side tasks. It supports a wide range of web development frameworks, such as Laravel, Symfony, and CodeIgniter, which provide additional tools and features for building web applications. PHP is an open-source language with a large community, extensive documentation, and a rich ecosystem of libraries and extensions.

How to check if URL Contain Certain String using PHP

Using strpos()function

The strpos() function in PHP is used to find the position of the first occurrence of a substring within 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).

Syntax

int strpos( $String, $Substring )

$String: This parameter holds the text where searching perform.

$SubstringThis parameter holds the pattern or sub string which is to be searched.

Example

<?php
$url = "https://www.tutorialspoint.com/php/";
// Check if the URL contains the string "example"
if (strpos($url, "tutor") !== false) {
   echo "The URL contains the string 'tutor'.";
} else {
   echo "The URL does not contain the string 'tutor'.";
}
// Another search substring
$key = 'hyderabad';
if (strpos($url, $key) == false) {
   echo $key . ' does not exists in the URL.';
}
else {
   echo $key . ' exists in the URL.';
}
?>

Output

The URL contains the string 'tutor'.hyderabad does not exists in the URL.

Using preg_match() function

The preg_match() function in PHP is used for pattern matching using regular expressions. It allows you to check if a pattern exists within a string.

Syntax

preg_match( $pattern, $subject )

Parameters

$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.

Example

<?php
// PHP program to find exach match substring
// Given a URL
$url = 'https://www.google.co.in/';
// Here '\b' represents the block
// This pattern search gfg as whole words
$pattern = '/\bgoogle\b/';
if (preg_match($pattern, $url) == false) {
	echo 'google does not exist in the URL. <br>';
} else {
	echo 'google exist in the URL .<br>';
}
// Given another URL
$url2 = 'https://www.google.co.in/';
// This pattern search function as whole words
$pattern = '/\bchrome\b/';
if (preg_match($pattern, $url2) == false) {
	echo 'chrome does not exist in the URL.';
} else {
	'chrome exist in the URL.';
}
?>

Output

google exist in the URL.
chrome does not exist in the URL.

Conclusion

In Summary, To check if a URL contains a certain string in PHP, you can use either the strpos() or preg_match() functions. The strpos() function searches for a substring within a string and returns the position of its first occurrence or false if not found. It is suitable for simple string matching. For example, strpos($url, $substring) can be used to check if a URL contains a specific string.

On the other hand, preg_match() allows for pattern matching using regular expressions. It searches for a pattern within a string and returns the number of matches or false if no match is found. Regular expressions offer more flexibility and advanced pattern matching capabilities. For instance, preg_match("/pattern/", $url) can be used to check if a URL contains a certain pattern. Both functions are useful for URL matching, but preg_match() provides more powerful pattern matching abilities, while strpos() is simpler and faster for basic string matching.

Updated on: 31-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements