Wednesday, May 8, 2024
HomeLanguagesPhpHow to check if URL contain certain string using PHP?

How to check if URL contain certain string using PHP?

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.

Method 1:
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).

Syntax:

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.




<?php
// PHP program to find certain substring in an URL
  
// Given URL
  
// Search substring 
$key = 'gfg';
  
if (strpos($url, $key) == false) {
    echo $key . ' not exists in the URL <br>';
}
else {
    echo $key . ' exists in the URL <br>';
}
  
// Another search substring
$key = 'function';
  
if (strpos($url, $key) == false) {
    echo $key . ' not exists in the URL';
}
else {
    echo $key . ' exists in the URL';
}
  
?>


Output:

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.neveropen.co.za/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.

Method 2:
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.

Syntax:

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




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


Output:

gfg exist in the URL 
function does not exist in the URL

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

14 COMMENTS

  1. Pretty section of content. I just stumbled upon your website and in accession capital to assert that I acquire actually enjoyed
    account your blog posts. Any way I’ll be subscribing to your feeds and even I achievement
    you access consistently fast.

  2. Hello there, just became alert to your blog through Google, and found that it’s truly informative.

    I am gonna watch out for brussels. I’ll appreciate if you continue this in future.
    Numerous people will be benefited from your writing. Cheers!

  3. I’m really loving the theme/design of your blog.
    Do you ever run into any web browser compatibility problems?
    A small number of my blog audience have complained about my site
    not working correctly in Explorer but looks great in Firefox.
    Do you have any tips to help fix this issue?

  4. Excellent weblog right here! Also your web site a lot up very fast!
    What web host are you using? Can I am getting your affiliate hyperlink to your host?
    I wish my web site loaded up as quickly as yours lol

  5. I truly love your site.. Very nice colors & theme.
    Did you develop this amazing site yourself? Please reply back as I’m hoping to create my own site and
    want to learn where you got this from or just what the theme is
    named. Thanks!

  6. Hi there very nice site!! Guy .. Beautiful ..
    Wonderful .. I will bookmark your blog and take the feeds additionally?
    I’m happy to seek out a lot of useful information here
    in the submit, we need develop more techniques on this regard, thank you for sharing.
    . . . . .

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments