- var_dump: documentation ( source)
- token_get_all: documentation ( source)
- preg_match: documentation ( source)
- reset: documentation ( source)
<?php
function isPhpKeyword($testString) {
// First check it's actually a word and not an expression/number/empty string
if (!preg_match('/^[a-z]+$/i', $testString)) {
return false;
}
$tokenised = token_get_all('<?php ' . $testString . '; ?>');
// tokenised[0] = opening PHP tag, tokenised[1] = our test string
return reset($tokenised[1]) !== T_STRING;
}
var_dump(
isPhpKeyword('if'),
isPhpKeyword('flibble'),
isPhpKeyword('while')
);