How to Check the URL Contains Certain String or Not Using PHP

Summarize

Git is a distributed version control system DVCS designed for efficient source code management, suitable for both small and large projects. It allows multiple developers to work on a project simultaneously without overwriting changes, supporting collaborative work, continuous integration, and deployment. This Git and GitHub tutorial is designed for beginners to learn fundamentals and advanced concepts, including branching, pushing, merging conflicts, and essential Git commands. Prerequisites include familiarity with the command line interface CLI, a text editor, and basic programming concepts. Git was developed by Linus Torvalds for Linux kernel development and tracks changes, manages versions, and enables collaboration among developers. It provides a complete backup of project history in a repository. GitHub is a hosting service for Git repositories, facilitating project access, collaboration, and version control. The tutorial covers topics such as Git installation, repository creation, Git Bash usage, managing branches, resolving conflicts, and working with platforms like Bitbucket and GitHub. The text is a comprehensive guide to using Git and GitHub, covering a wide range of topics. It includes instructions on working directories, using submodules, writing good commit messages, deleting local repositories, and understanding Git workflows like Git Flow versus GitHub Flow. There are sections on packfiles, garbage collection, and the differences between concepts like HEAD, working tree, and index. Installation instructions for Git across various platforms Ubuntu, macOS, Windows, Raspberry Pi, Termux, etc. are provided, along with credential setup. The guide explains essential Git commands, their usage, and advanced topics like debugging, merging, rebasing, patch operations, hooks, subtree, filtering commit history, and handling merge conflicts. It also covers managing branches, syncing forks, searching errors, and differences between various Git operations e.g., push origin vs. push origin master, merging vs. rebasing. The text provides a comprehensive guide on using Git and GitHub. It covers creating repositories, adding code of conduct, forking and cloning projects, and adding various media files to a repository. The text explains how to push projects, handle authentication issues, solve common Git problems, and manage repositories. It discusses using different IDEs like VSCode, Android Studio, and PyCharm, for Git operations, including creating branches and pull requests. Additionally, it details deploying applications to platforms like Heroku and Firebase, publishing static websites on GitHub Pages, and collaborating on GitHub. Other topics include the use of Git with R and Eclipse, configuring OAuth apps, generating personal access tokens, and setting up GitLab repositories. The text covers various topics related to Git, GitHub, and other version control systems Key Pointers Git is a distributed version control system DVCS for source code management. Supports collaboration, continuous integration, and deployment. Suitable for both small and large projects. Developed by Linus Torvalds for Linux kernel development. Tracks changes, manages versions, and provides complete project history. GitHub is a hosting service for Git repositories. Tutorial covers Git and GitHub fundamentals and advanced concepts. Includes instructions on installation, repository creation, and Git Bash usage. Explains managing branches, resolving conflicts, and using platforms like Bitbucket and GitHub. Covers working directories, submodules, commit messages, and Git workflows. Details packfiles, garbage collection, and Git concepts HEAD, working tree, index. Provides Git installation instructions for various platforms. Explains essential Git commands and advanced topics debugging, merging, rebasing. Covers branch management, syncing forks, and differences between Git operations. Discusses using different IDEs for Git operations and deploying applications. Details using Git with R, Eclipse, and setting up GitLab repositories. Explains CI/CD processes and using GitHub Actions. Covers internal workings of Git and its decentralized model. Highlights differences between Git version control system and GitHub hosting platform.

2 trials left

Hey Friends, In this tutorial 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 1: 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
$url = 'https://codegyan.in/hey/index';
  
// Search substring 
$key = 'hey';
  
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:

hey 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://codegyan.in/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
$url = 'https://codegyan.in/hey/myfunction';
  
// Here '\b' represents the block
  
// This pattern search hey as whole words
$pattern = '/\bhey\b/';
  
if (preg_match($pattern, $url) == false) {
    echo 'hey does not exist in the URL <br>';
}
else {
    echo 'hey exist in the URL <br>';
}
  
// Given another URL
$url2 = 'https://codegyan.in/myfunction';
  
// 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:

hey exist in the URL 
function does not exist in the URL

You may also like this!