Home » How to check if string contains substring in PHP?

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.

Read Also:  How to convert base64 to image and save in folder php?
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).

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

Read Also:  How to String Replace in PHP Functions?

<?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 Remove all special characters from a string in php?

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


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Carbon format Live datetime Example
  2. Read Also:  Get maximum key value of array in PHP
  3. Read Also:  How To Convert String Replace In Php Examples
  4. Read Also:  PHP String Email word-char limit timestamp Examples
  5. Read Also:  PHP Upload File Size Limit
  6. Read Also:  Solution for "Trying to get property of non-object" in Laravel
  7. Read Also:  Disable Crontab Email Notifications suppress cron
  8. Read Also:  ajax live data search using jquery php mysql
  9. Read Also:  Laravel Set cron job in cpanel Scheduling Tutorial
  10. Read Also:  how to store json data in mysql using php?

Leave a Comment

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