检查字符串末尾是否存在任何数组值
我正在尝试测试一个由多个单词组成的字符串,并且其末尾是否有数组中的任何值。以下是我到目前为止所拥有的。我陷入了如何检查字符串是否比正在测试的数组值长以及它是否出现在字符串末尾的问题上。
$words = trim(preg_replace('/\s+/',' ', $string));
$words = explode(' ', $words);
$words = count($words);
if ($words > 2) {
// Check if $string ends with any of the following
$test_array = array();
$test_array[0] = 'Wizard';
$test_array[1] = 'Wizard?';
$test_array[2] = '/Wizard';
$test_array[4] = '/Wizard?';
// Stuck here
if ($string is longer than $test_array and $test_array is found at the end of the string) {
Do stuff;
}
}
I am trying to test if a string made up of multiple words and has any values from an array at the end of it. The following is what I have so far. I am stuck on how to check if the string is longer than the array value being tested and that it is present at the end of the string.
$words = trim(preg_replace('/\s+/',' ', $string));
$words = explode(' ', $words);
$words = count($words);
if ($words > 2) {
// Check if $string ends with any of the following
$test_array = array();
$test_array[0] = 'Wizard';
$test_array[1] = 'Wizard?';
$test_array[2] = '/Wizard';
$test_array[4] = '/Wizard?';
// Stuck here
if ($string is longer than $test_array and $test_array is found at the end of the string) {
Do stuff;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串结尾是指最后一个单词吗?你可以使用 preg_match
By end of string do you mean the very last word? You could use preg_match
我认为你想要这样的东西:
如果它必须是一个任意数组(而不是包含你在问题中提供的“向导”字符串的数组),你可以动态构造正则表达式:
I think you want something like this:
If it has to be an arbitrary array (and not the one containing the 'wizard' strings you provided in your question), you could construct the regex dynamically:
这是你想要的吗(不保证正确性,无法测试)?
我想知道一些正则表达式在这里是否更有意义。
Is this what you want (no guarantee for correctness, couldn't test)?
I wonder if some regular expression wouldn't make more sense here.