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?
- Transaction when using WP functions rather than vanilla SQL?
- How to run multiple Async HTTP requests in WordPress?
- Can i change wordpress emoji src from http://s.w.org/ to my site?
- WordPress theme & site not loading after moving files
- How to merge local and live databases?
- WP_Query use for a filter with multiple Taxonomies and Terms
- WP Plugin + OOP: Adding Menu Page doesn’t create required effect
- Can I run custom php on specific pages in wordpress?
- Display All Post Attachments and Assign Class to the Last Image?
- Newbie question: no index.php? in my plain wordpress permalink
- enqueue hover function
- Style wp_page_menu() items
- Proper use of internationalization
- How to update WP about renamed files
- How to sanitize my cookie name
- How to select WooCommerce products by post_meta and order them
- If search results empty then execute certain code
- Any possible way to make $wpdb->get_results() return anything else than array?
- How to stop featured image thumbnail [set as background image] on blog index page just repeating same image across all posts
- Change list item content in menu navigation to add a child element with specific class
- wordpress count link clicks by ip address
- “comments.php” not available after edit in WP administration
- php script to change slug in post automatically
- Repeating admin-ajax.php not found error in admin
- Remove a div from RSS feed
- Add more button if more 8 items
- Child page menu in sidebar
- check if comment submitted was successful
- Thumbnails appear to be zoomed
- Download images from media library to computer in BULK?
- Detecting if parent page has gallery
- Why is an array created in a function hooked to customize register populated when customizer is loaded but not when the front-end is loaded?
- Location of core code for database connection and get_header
- Removing WordPress Footer -without access to PHP code?
- Upload non-featured image to image field
- How to get specific attribute from DB
- customize woocommerce templates and display store on home
- Plugin development and composer
- WC_Customer delete function returns error
- Increment paged on WP_Query
- Delete or edit postmeta with specific meta_key?
- Post repeated when opening in overlay, how do I solve this? [closed]
- Woocommerce: remove total sum from new order’s email [closed]
- Trying to display text in a block in a plugin page
- Some menu items wont budge
- Removing “Powered by” footer using child theme PHP [closed]
- custom post type with role Vendor
- how to hide or disable some part in specify page
- Cannot load media and I get “PHP Warning: Invalid argument supplied for foreach(“
- Adding “redirect” to a button
- Include a file that has a function in it
- Trying to split a query into sections of six
- Kohana Frontend, WordPress Backend
- making my own “related pages” / “pages you might like” section
- Show ranking based on post id
- Add Custom Category Description When Empty
- Contain multiple page templates in one PHP custom template file in WordPress?
- url not using query string no longer working
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Display div if category has a specific parent
- Custom plugin: how do I call a PHP file if settings option is set to true?
- I want it to detect the duplicate comment and give a warning on the same page
- How to identify which php file a plugin is using on page load?
- How to edit button permalink inside function.php using a child theme?
- How to switch between two primary menus (programatically0
- Why am I getting syntax error, unexpected ‘endwhile’ (T_ENDWHILE) on single.php
- Elementor to custom wordpress page
- custom post type not showed with bootstrap 4 modal and template part
- Remove item from post_meta array via AJAX
- Shortcode just with a php code
- Calling WordPress hook from within a class: call to undefined function error
- wp_send_json erase last value
- PHP array to jQuery array ” Uncaught SyntaxError: Unexpected token < "
- I want show only data for today
- How can I customize a WordPress theme before it’s downloaded?
- Permalinks and custom PHP application
- Updating meta_value in a custom key
- rewrite_rule – working fine but broken for pagination
- AdWords API with WP
- Add title and caption to thumbnail image on mouseover
- Postmeta not saving
- WordPress wp_remote_post() sending informaiton to Redmine REST API
- Missing items on page
- WordPress rewrite question
- Using Nonce for my Form
- WordPress blog set up
- Cannot get custom javascript to execute on page
- How to declare a variable at the bottom of the page and use its value at the top?
- Shortcode working in page.php but not in category.php in wordpress
- How can a ‘scripts’ directory be hooked into wp_head();?
- Display Current Time using shortcode
- WP and Laravel integration (Updated) [closed]
- PHP $_SESSION array – only showing current index
- How can I use AJAX in child theme template?