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


int strpos( $String, $Substring )

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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

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

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

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


Web Programming Tutorials Example with Demo

Read :


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

Related FAQ

Here are some more FAQ related to this Article:

  1.   how to compare two strings in PHP if condition?
  2.   Simple PHP Rating System with database using JavaScript
  3.   PHP Dynamically generate QR code Library
  4.   Get maximum key value of array in PHP
  5.   Disable Crontab Email Notifications suppress cron
  6.   laravel findOrFail Eloquent Query
  7.   PHP Check If String Contains Substring
  8.   Laravel 6 concatenate Multiple Columns with Example
  9.   How to Check If String Contains a Substring in PHP
  10.   Jquery simple rating system with star

Leave a Reply

Your email address will not be published. Required fields are marked *