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?
- Array to String PHP?
- Mysql query- How to use contains?
- how to replace quotation marks with \”
- PHP random string generator
- Is a new line = \n OR \r\n?
- Mixing a PHP variable with a string literal
- Converting string to Date and DateTime
- PHP: variables in strings without concatenation
- How do I check if string contains substring? [duplicate]
- How do I check if string contains substring?
- Multi-line strings in PHP
- Uncaught Error: Call to undefined function mysql_escape_string()
- How can I capture the result of var_dump to a string?
- How to convert string to boolean php
- Check if URL has certain string with PHP
- Limit String Length
- Insert string at specified position
- Remove empty array elements
- How to set 777 permission on a particular folder? [closed]
- Extract substring in Bash
- regex match any whitespace
- PHP page redirect
- How do I resolve a HTTP 414 “Request URI too long” error?
- what does PHP die() return
- Invalid column count in CSV input on line 1 Error
- mcrypt is deprecated, what is the alternative?
- How to echo or print an array in PHP?
- Resize image in PHP
- Java – removing first character of a string
- How to replace “if” statement with a ternary operator ( ? : )?
- why use 0xffff over 65535
- .rar, .zip files MIME Type
- PDO bindParam() with prepared statement isn’t working
- HTML Element Array, name=”something[]” or name=”something”?
- Why use $_SERVER[‘PHP_SELF’] instead of “”
- Setting up a PHP interpreter in PhpStorm
- PHP: $_SERVER variables: $_SERVER[‘HTTP_HOST’] vs $_SERVER[‘SERVER_NAME’]
- Illegal string offset Warning PHP
- Fatal error: Call to undefined function mysqli_connect()
- Could not open input file: artisan
- Why php tag is not working in html file?
- What’s the use of ob_start() in php?
- What is the difference between the ‘www’ folder and ‘htdocs’ folder?
- Notice: Trying to get property of non-object error
- undefined offset PHP error
- PHP mail not working for some reason
- Get Root Directory Path of a PHP project
- Returning JSON from PHP to JavaScript?
- How to create cron job using PHP?
- Imploding an associative array in PHP
- curl POST format for CURLOPT_POSTFIELDS
- Can’t use method return value in write context
- PHP get domain name
- How to solve “Fatal error: Class ‘MySQLi’ not found”?
- Running a Python script from PHP
- Object of class DateTime could not be converted to string
- HTTP Error 411, The request must be chunked or have a content length
- mysqli::query(): Couldn’t fetch mysqli
- Fatal error: Call to undefined function sqlsrv_connect()
- NotFoundHttpException in RouteCollection.php line 161: in laravel 5
- Easy way to password-protect php page
- Html encode in PHP
- file_put_contents: Failed to open stream, no such file or directory
- PHP json_decode() returns NULL with valid JSON?
- Why do I have to run “composer dump-autoload” command to make migrations work in laravel?
- PHP $_POST not working?
- PHP cURL HTTP PUT
- Convert timestamp to readable date/time PHP
- PHP sessions that have already been started
- Contact Form 7 – Custom Validation
- How to output JavaScript with PHP
- WRONGTYPE Operation against a key holding the wrong kind of value php
- What is the advantage of using try {} catch {} versus if {} else {}
- Subtracting days, months or years from date using php
- Laravel – Model Class not found
- Fatal error: Call to a member function fetch_assoc() on a non-object
- Strict Standards: Only variables should be assigned by reference PHP 5.4
- mysqlworkbench giving version error on exporting database
- Steam Market API?
- PHP array delete by value (not key)
- PHP mail: Multiple recipients?
- Having a problem getting mysqli_query to execute
- PHP Multiple Checkbox Array
- Session variables not working php
- How get value from URL
- Get JSON object from URL
- How can I parse a JSON file with PHP? [duplicate]
- Object of class stdClass could not be converted to string
- How to get URL of current page in PHP
- How to push both value and key into PHP array
- WordPress visual composer not loading
- Find the last element of an array while using a foreach loop in PHP
- Popup window in PHP?
- How can you use php in a javascript function
- How to bypass (deprecated) reCAPTCHA V1?
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ””)’ at line 2