How to check if string contains substring in PHP?

Question: How to Check If a String Contains a Specific Word in PHP?, How to check if URL contain certain strings using PHP?

This Snippet will help you to check if a string contains any substring in PHP (server-side scripting language) Web programming language. For simple example, you want to execute a particular text line of source code only if user an input string contains additional get substring in it.

Answer: Use the PHP strpos() Function

We can use the inbuilt PHP strpos() methods to check and get whether a data string contains a particular word or not exist.

php string contains,php if string contains,php contains,php string contains space,searching string in php,strpos php,php get substring,php string replace,php contains array,php startswith,php strpos array
How to check if strings contains substring in PHP

Syntax:

int strpos( $String, $Substring )

check php if strings accommodate Flowchart :

pos search, php position, php search string, php strpos, php check, 1 man 2 needles, needle haystack, str.match, php this, string contains, findstr examples, string find, strstr php, php string starts with, php find string in string
check php if string contains Flowchart

Example 1 : php check strings

The below source code will strpos determine to true since the primary data string $stringcontain contains data the substring ‘Welcome‘ in it. This will PHP print “true” (return boolean value).

Read Also:  How to convert string number into month name in PHP

<?php
$stringcontain = 'Welcome to a brand new series on web development';
 
if (strpos($stringcontain, 'Welcome') !== false) {
	//return boolean value
    echo 'true';
}
?>

Example 2 : php check if string

The below source code will determine to false since the primary data string $stringcontain doesn’t have data the substring ‘Ejunkie‘ in it(using php strpos array). Welcome will nothing PHP print.

<?php
$stringcontain = 'Welcome to a brand new series on web development';
$substr = "Ejunkie";
 
if (strpos($stringcontain, $substr) !== false) {
	//return boolean value
    echo 'true';
}
?>

Example 3. String Contains Substring on Start

The below source code will check if a String contains a substring at start. The below source code will determine to true (return boolean value) since the primary data string $stringcontain take in data the substring ‘Welcome‘ at start.

Read Also:  How To Convert String Replace In Php Examples

<?php
$stringcontain = 'Welcome to a brand new series on web development';
 
if (strpos($stringcontain, 'Welcome') === 0 ) {
	//return boolean value
    echo 'true';
}
?>

Ref: https://www.php.net/manual/en/function.strpos.php

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about php Text accommodate space.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.