|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-07-16 03:52 UTC] thbley at gmail dot com
Description:
------------
~ = matches substring (contains operator)
^~ = matches beginning of string (begins with operator)
~^ = matches ending of string (ends with operator)
$str = "abcdef";
old:
if (strpos($str, "bcd")!==false) {} // true
if (strpos($str, "abc")===0) {} // true
if (strpos($str, "def")===3) {} // true
new:
if ($str ~ "bcd") {} // true
if ($str ^~ "abc") {} // true
if ($str ~^ "def") {} // true
Test script:
---------------
$str = "abcdef";
assertTrue($str ~ "bcd");
assertTrue($str ^~ "abc");
assertTrue($str ~^ "def");
assertFalse($str ~ "aef");
assertFalse($str ^~ "bcd");
assertFalse($str ~^ "abc");
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 16:00:01 2025 UTC |
It does not make any sense to introduce new operators just to save five characters. If you have to do this really often and/or find strpos unreadable, you can simply define a few helper functions: function contains($str1, $str2) { return false !== strpos($str1, $str2); } function startsWith($str1, $str2) { return 0 === strpos($str1, $str2); } function endsWith($str1, $str2) { return 0 === substr_compare($str1, $str2, -strlen($str2), strlen($str2)); } They have the additional advantage of being more clear than something like ~^.