Skip to main content

Timeline for answer to How do I check if a string contains a specific word? by codaddict

Current License: CC BY-SA 4.0

Post Revisions

63 events
when toggle format what by license comment
Jan 11, 2025 at 23:24 comment added Jake @Melsi, I use === and !== whenever possible. But sometimes in some frameworks (e.g. WordPress), numbers come out of the database as strings (by the time you get hold of them); in such situations == 0 may be judicious if you can't reliably ascertain whether the value might be 0 as an integer or a string containing 0 ('0').
Nov 13, 2023 at 21:13 comment added What is the rationale behind the empty $needle string behavior with PHP 8 str_contains?
Aug 17, 2023 at 17:39 history edited Stas Trefilov CC BY-SA 4.0
Fix check for empty $needle
Jul 21, 2023 at 18:40 history notice added hakre Recommended answer in PHP
Jun 4, 2023 at 19:58 history edited Mister Verleg CC BY-SA 4.0
MAde the first example more condensed.
Mar 13, 2023 at 6:06 history edited SeniorDeveloper CC BY-SA 4.0
Making the empty substring easier to understand.
Mar 13, 2023 at 6:01 history edited SeniorDeveloper CC BY-SA 4.0
Adding more info on the new PHP8 str_contains functionality so others can understand how to use it properly.
Mar 13, 2023 at 5:54 history edited SeniorDeveloper CC BY-SA 4.0
added 342 characters in body
Mar 13, 2023 at 5:46 history edited SeniorDeveloper CC BY-SA 4.0
added 639 characters in body
Jan 25, 2023 at 14:19 comment added V. Bozz @Giulio_Muscarello if you want to share an answer using regexes as a solution, do it. In the meantime please remove your comment (since it's not related to this answer but another not existing answer) and mark my comment as "not useful anymore". Otherwise any people can ask "why not xxx?" in any comment. By the way here an answer using regexes and explaining that: stackoverflow.com/a/4366744/3451846
Aug 26, 2022 at 14:55 history edited Daniel L. VanDenBosch CC BY-SA 4.0
Specified needle and haystack
Jun 2, 2022 at 7:27 comment added stmax Funnily strpos('foo', 'bar') > -1 gives false, strpos('foo', 'bar') >= 0 gives true. Even funnier php getting a sane str_contains only in version 8.. about time.
Apr 1, 2022 at 1:39 history edited manlikeangus CC BY-SA 4.0
Changed link to official PHP documentation
Jan 28, 2022 at 12:09 history edited Jannick Breunis CC BY-SA 4.0
improved answer on top
Jul 14, 2021 at 16:36 comment added ToolmakerSteve @baptx - equally importantly, its easier to not screw up. As the answer describes, slight variations on the test will give the wrong result on "falsey" strings. Because of this, the sane thing to do, is to define a function.
May 21, 2021 at 21:04 comment added baptx So the advantage of str_contains is just that it is easier to read?
Apr 12, 2021 at 16:13 comment added squarecandy If you're still on PHP 7 but want to start using the PHP 8 function, you can use this polyfill: if (!function_exists('str_contains')) { function str_contains($haystack, $needle) { return $needle !== '' && mb_strpos($haystack, $needle) !== false; } } -- taken from this php.net comment; based on Laravel.
Mar 18, 2021 at 16:56 history edited Dharman CC BY-SA 4.0
Emphasize the new function
S Jun 29, 2020 at 11:06 history suggested Awais CC BY-SA 4.0
strpos equivalent in PHP 8
Jun 29, 2020 at 3:14 review Suggested edits
S Jun 29, 2020 at 11:06
Nov 11, 2019 at 22:46 history edited dreftymac CC BY-SA 4.0
minor edit
Aug 22, 2019 at 4:36 review Suggested edits
Aug 22, 2019 at 6:31
Jul 10, 2019 at 8:21 history edited Thomas W. CC BY-SA 4.0
Add additional clarification from comments
Feb 1, 2019 at 16:52 history edited Mystical CC BY-SA 4.0
removed unnecessary backslash that obscures the intent of the expression
Jan 18, 2019 at 16:05 history edited Eugene Kaurov CC BY-SA 4.0
I added using '\' namespace since it increase the productivity
May 4, 2018 at 6:13 history made wiki Post Made Community Wiki by samliew
Mar 8, 2018 at 10:38 review Suggested edits
Mar 8, 2018 at 11:12
Feb 26, 2018 at 19:37 comment added Code4R7 If you are interested in words rather than bytes, use grapheme_strpos(). Or, if you really can not use Intl, use mb_strpos() instead.
Jan 12, 2018 at 0:04 comment added Ron If I get $1 everytime I visit this page just to copy paste this solution, I could have bought ramen noodles for a whole week
Jan 2, 2018 at 8:58 comment added Tim Visée Note, that because many frameworks agree this is dumb, most of them have helper functions available. For example, Laravel has str_contains($a, 'are'); as seen here: laravel.com/docs/5.5/helpers#method-str-contains.
Dec 14, 2017 at 11:16 comment added Mark C. Yes, using '===' is faster than '==' because there is no type coercion necessary.
S Aug 7, 2017 at 13:49 history edited Massimiliano Kraus CC BY-SA 3.0
Grammar, strpos-es replaced with strpos()
S Aug 7, 2017 at 13:49 history suggested zvava CC BY-SA 3.0
Grammar, strpos-es replaced with strpos()
Aug 7, 2017 at 12:50 review Suggested edits
S Aug 7, 2017 at 13:49
Jun 9, 2017 at 13:01 comment added Djave I've landed on this specific answer hundreds of times in my career, and every time I read it, my brain hurts. Seeings as this question has been view 2.5 million times could we possibly change the example to something like $subject = 'How are you?';$query = 'are';if (strpos($subject, $query) !== false).... No worries if not, the fact it has been viewed 2.5 million times is probably also a good reason not to change it. I'd also opt for return true rather than echo 'true' but I understand that is really starting to deviate from the question.
Mar 3, 2017 at 17:33 comment added LIGHT why not simply if (strpos($a, 'are') > -1) {//found}else{//not found}
Dec 5, 2016 at 15:38 review Suggested edits
Dec 5, 2016 at 16:20
Oct 20, 2016 at 5:09 review Suggested edits
Oct 20, 2016 at 6:22
S Jun 30, 2016 at 18:24 history suggested Tijme CC BY-SA 3.0
Made it more clear which parameter is the needle and which is the haystack.
Jun 30, 2016 at 16:02 review Suggested edits
S Jun 30, 2016 at 18:24
May 31, 2016 at 11:55 review Suggested edits
May 31, 2016 at 12:29
Feb 1, 2016 at 11:59 comment added xDaizu @DTest is kinda right. I don't want to be "that guy" but either this answer is incomplete or the question should be rephrased to not specify it's looking for "words" ^^U
Jan 22, 2016 at 10:44 history edited Idrizi.A CC BY-SA 3.0
added 1 character in body
Nov 5, 2014 at 0:37 comment added minipif @Wouter Padding with spaces and searching for " are " is not the solution, as it's not necessarily followed by a space (eg. " You are. ")
May 6, 2014 at 6:01 comment added equazcion I tend to avoid this issue by always using strpos($a, 'are') > -1 to test for true. From a debugging perspective, I find my brain wastes fewer clock cycles determining if the line is written correctly when I don't have to count contiguous equals signs.
Apr 9, 2014 at 14:08 comment added trejder @Melsi What speed got to do with the fact, whether you use compare with (===) or without (==) type checking? Do you suggest, that === is faster that ==? I doubt so...
Feb 20, 2014 at 21:13 comment added Tino @EchtEinfachTV try strpos($a,'are')===false. === is the complementary operator of !==.
Feb 20, 2014 at 16:27 comment added meda @EchtEinfachTV that function always return false or the postion but never true
Feb 4, 2014 at 21:54 review Suggested edits
Feb 4, 2014 at 21:57
Dec 31, 2013 at 7:32 comment added Avatar I wanted to check if a string does not contain a word. I tried to change false to true if (strpos($a,'are')!==true) {...} but it does not work. Instead I am using now: if(! (strpos($a,'are')!==false)) { ... } which looks awkward. Anyone?
Sep 23, 2013 at 14:26 comment added Wouter As for not catching 'care' and such things, it is better to check for (strpos(' ' . strtolower($a) . ' ', ' are ') !== false)
Jul 17, 2013 at 12:26 comment added Martijn @Guiulio: Regex is slower :) If you only need to check if it exists, in any way ( so 'are' or 'care' doesnt matter), then use basic functions for better performance
Jun 6, 2013 at 3:17 history edited Cairnarvon CC BY-SA 3.0
Clarification in light of the *two* recent incorrect suggested edits.
Jun 6, 2013 at 3:10 review Suggested edits
Jun 6, 2013 at 3:13
Jun 6, 2013 at 3:03 review Suggested edits
Jun 6, 2013 at 3:08
Jan 8, 2013 at 19:20 comment added erdomester best way is if ((strpos($form_email,'@') === false) || (strpos($form_email,'.') === false)) { $error = 'Invalid email<br>'; }
Jan 6, 2013 at 15:48 comment added Giulio Muscarello @jsherk Why not regexes, then? Something like " are ".
Dec 15, 2012 at 12:28 comment added Melsi Very good comments above! I never use != or ==, after all !== and === is best option (in my opinion) all aspect considered (speed, accuracy etc).
Nov 14, 2012 at 21:35 comment added jsherk @DTest - well yes of course it will return true because the string contains 'are'. If you are looking specifically for the word ARE then you would need to do more checks like, for example, check if there is a character or a space before the A and after the E.
Dec 6, 2010 at 13:31 history edited codaddict CC BY-SA 2.5
edited body
Dec 6, 2010 at 13:29 vote accept Charles Yeung
Dec 6, 2010 at 13:29 vote accept Charles Yeung
Dec 6, 2010 at 13:29
Dec 6, 2010 at 13:15 history answered codaddict CC BY-SA 2.5