note 114687 added to function.strpos
| From: | m at binh dot vn | Date: | Sat, 22 Mar 2014 09:04:03 +0000 |
| Subject: | note 114687 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199012@lists.php.net to get a copy of this message | ||
Using strpos can be tricky sometimes. To reuse it for one search you can use this form:
<?php
$found = (strpos($haystack, $needle) !== false);
?>
For example:
<?php
$url = "http://www.binh.vn";
$found = (strpos($url, 'http') !== false);
if ($found) echo "Found http";
?>
Clearly we see that the position will be 0 but the $found will be true and so we can use more than
one time of if ($found) which is much easier.
To further reuse it we could make a very short function:
<?php
function found($haystack, $needle) {
return (strpos($haystack, $needle) !== false);
}
?>
Wonder why php didn't have such function for easier use?
----
Server IP: 72.52.91.14
Probable Submitter: 42.113.166.72
----
Manual Page -- http://php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/114687
Del: integrated -- https://master.php.net/note/delete/114687/integrated
Del: useless -- https://master.php.net/note/delete/114687/useless
Del: bad code -- https://master.php.net/note/delete/114687/bad+code
Del: spam -- https://master.php.net/note/delete/114687/spam
Del: non-english -- https://master.php.net/note/delete/114687/non-english
Del: in docs -- https://master.php.net/note/delete/114687/in+docs
Del: other reasons-- https://master.php.net/note/delete/114687
Reject -- https://master.php.net/note/reject/114687
Search -- https://master.php.net/manage/user-notes.php