Change language

How can I check if a URL contains a specific string using PHP?

|
Method 1:
The strpos() function:The strpos() function is used to find the first occurrence of a substring in a string. If the substring exists, the function returns the starting index of the substring, otherwise it returns False if the substring is not found in the string (URL).Syntax:
int strpos ( $String, $Substring)
Parameters:The strpos() function takes two parameters as above and described below.
  • $String:this parameter contains the text to be searched.
  • $Substring:this parameter contains the pattern or substring to find.
Program:PHP program for finding a specific string in a URL using the strpos() function.
// PHP program for finding a specific substring in a URL 
// given URL $url = https://www.engineerforengineer.org/gfg/index ;  
// Find a substring $key = ’gfg’ ; if ( strpos ( $url , $key ) == false) { echo $key . ’not exists in the URL
; } else { echo $key . ’exists in the URL
; }  
// Another search string $key = ’function’ ; if ( strpos ( $url , $key ) == false) { echo $key . ’not exists in the URL’ ; } else { echo $key . ’exists in the URL’ ; }  
?>
Exit:
gfg exists in the URL 
function not exists in the URL
Note.The strpos() function finds a substring in the text using the string matching method. This sometimes gives undesirable results. For example: if the string url is https://python.engineering/myfunction, and the substring is function, then the substring exists in the string url. Suppose a website wants to display the result of a function, but it displays a result of myfunction, which is different. The strpos() function does not check if the entire substring is present, or with a suffix or prefix.Note.To solve this problem, you need to find out if the exact pattern is present in the string (URL) or no preg_match() function.Method 2:
preg_match() function:The preg_match() function is used to find an exact match for a pattern in the text using a regular expression search. Here, given the regular expression pattern, the function searches through the text and finds an exact match, if any. This function returns true if the pattern is present and false if there is no pattern.Syntax:
preg_match ($pattern, $subject)  Parameters:The preg_match() function takes two parameters as above and described below.
  • $pattern:is regex patternto search as a string
  • $subject:is the text string against which the regex pattern is searched.
Program 2:PHP program for finding an exact match of a string in a URL
// PHP program for finding a substring match 
// URL given $url =  https://www.engineerforengineer.org/ gfg / myfunction ;  
// Here ’ / b ’ represents block 
// This GFG search pattern as whole words $pattern = ’/ gfg /’ ; if (preg_match ( $pattern , $url ) == false) { echo ’gfg does not exist in the URL
; } else { echo ’ gfg exist in the URL
; }  
// different URL given $url2 = https://www.engineerforengineer.org/myfunction ;  
// This function will search the pattern as whole words $pattern = ’/ function /’ ; if (preg_match ( $pattern , $url2 ) == false) { echo ’function does not exist in the URL’ ; } else { ’ function exist in the URL’ ; }  
?>
Exit:
gfg exist in the URL 
function does not exist in the URL

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method