I have system, that using keywords for some data
There are normal keywords and meta keywords - To:all, Tomember: and Togroup:
and I have following condition to check meta keywords:
if ((strpos($kwd, 'To:all') === 0) ||
(strpos($kwd, 'Tomember:') === 0) ||
(strpos($kwd, 'Togroup:') === 0))
{
/* ... */
}
I think this way of identifying meta keywords is incorrect.
One more incorrect way is like this:
if ((strpos($kwd, 'To:all') !== FALSE) ||
(strpos($kwd, 'Tomember:') !== FALSE) ||
(strpos($kwd, 'Togroup:') !== FALSE))
{
/* ... */
}
And in my opinion the correct way is:
if ((substr($kwd,0,6) == 'To:all') ||
(substr($kwd,0,9) == 'Tomember:') ||
(substr($kwd,0,8) == 'Togroup:'))
{
/* ... */
}
Any thoughts?
Of the solutions you propose, the second is wrong because it will return true even if the meta-keywords do not appear in the beginning of $kwd
. The other two work correctly.
An even better way would be:
function str_starts_with($haystack, $needle) {
return substr($haystack, 0, strlen($needle)) == $needle;
}
if (str_starts_with($kwd, 'To:all') ||
str_starts_with($kwd, 'Tomember:') ||
str_starts_with($kwd, 'Togroup:'))
{
// ...
}