You can use the strpos()
function which is used to find the occurrence of one string inside another one:
$a = 'How are you?'; if (strpos($a, 'are') !== false) { echo 'true'; }
Note that the use of !== false
is deliberate (neither != false
nor === true
will return the desired result); strpos()
returns either the offset at which the needle string begins in the haystack string, or the boolean false
if the needle isn’t found. Since 0 is a valid offset and 0 is “falsey”, we can’t use simpler constructs like !strpos($a, 'are')
.
Now with PHP 8 you can do this using str_contains:
if (str_contains('How are you', 'are')) { echo 'true'; }
Related Posts:
- How do I check if a string contains a specific word?
- Does Python have a string ‘contains’ substring method?
- How to check whether a string contains a substring in JavaScript?
- Does Python have a string ‘contains’ substring method?
- How do I check if string contains substring? [duplicate]
- How do I check if string contains substring?
- Convert hex color to RGB values in PHP
- Calculate business days
- How do I get a substring of a string in Python?
- Substring in excel
- How do I make a redirect in PHP?
- How to add elements to an empty array in PHP?
- What do ++ and *+ mean?
- Array to String PHP?
- What’s the net::ERR_HTTP2_PROTOCOL_ERROR about?
- Undefined function mysql_connect()
- MySQL column count doesn’t match value count at row 1 [closed]
- Extract substring in Bash
- Matching a space in regex
- Undefined function mysql_connect()
- How can I send an email using PHP?
- Fatal error: Call to undefined function mysql_connect()
- count() parameter must be an array or an object that implements countable in laravel
- Go Back to Previous Page
- How do I replace part of a string in PHP? [duplicate]
- How to declare a global variable in php?
- How to fix “Headers already sent” error in PHP
- Fatal error: Call to undefined function mysql_connect()
- Check if a string contains a string in C++
- How to fix ‘Notice: Undefined index:’ in PHP form action
- Loop through an array php
- Replace part of a string with another string
- Mysql query- How to use contains?
- Deprecated: mysql_connect()
- Where does PHP store the error log? (PHP 5, Apache, FastCGI, and cPanel)
- how to replace quotation marks with \”
- PHP random string generator
- What is the difference between String.subString() and String.subSequence()
- Is a new line = \n OR \r\n?
- Java – removing first character of a string
- How does String substring work in Swift
- Chrome net::ERR_INCOMPLETE_CHUNKED_ENCODING error
- Illegal string offset Warning PHP
- What is PHPSESSID?
- MySQL “Or” Condition
- What is the significance of the number, 32767?
- PHP – how to create a newline character?
- How to extract the substring between two markers?
- Mixing a PHP variable with a string literal
- How to add a line break within echo in PHP?
- Converting string to Date and DateTime
- PHP: variables in strings without concatenation
- \n or \n in php echo not print [duplicate]
- Multi-line strings in PHP
- Find the nth occurrence of substring in a string
- How to replace a substring of a string
- How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?
- Simple way to check if a string contains another string in C?
- Uncaught Error: Call to undefined function mysql_escape_string()
- How to read a large file line by line?
- How can I capture the result of var_dump to a string?
- Fastest way to remove first char in a String
- Substring index and length must refer to a location within the string
- PHP to write Tab Characters inside a file?
- How to convert string to boolean php
- Case insensitive ‘Contains(string)’
- Best way to do multiple constructors in PHP
- Remove last character from C++ string
- warning: ‘characters’ is deprecated: Please use String or Substring directly
- Check if URL has certain string with PHP
- Limit String Length
- How do I get the last character of a string?
- Insert string at specified position
- Remove empty array elements
- How to know if a given string is substring from another string in Java
- How to get URL of current page displayed?
- Undefined variable in functions.php file [closed]
- What are PHP extensions and libraries WP needs and/or uses?
- What is the correct way to use WordPress functions outside WordPress files?
- Allow HTML in excerpt
- How to get WordPress Time Zone setting?
- how to get page id of a page using page slug
- Check if current page is the Blog Page
- How to set and use global variables? Or why not to use them at all
- Sending the reset password link programatically
- How to add product in woocommerce with php code [closed]
- Show all terms of a custom taxonomy?
- How exactly do automatic updates work?
- what is correct way to hook when update post
- How to update custom fields using the wp_insert_post() function?
- How to create custom 401, 403 and 500 error pages?
- Why have on every line
- Most efficient way to add javascript file to specific post and/or pages?
- Running WP Cron on multisite the right way
- Getting only direct child pages in WordPress with get_pages
- the_date() not working
- How do I use WP_query with multiple post IDs?
- How to return number of found rows from SELECT query
- Query multiple custom post types in single loop
- How to include checkbox in widget backend form?