检查字符串末尾是否存在任何数组值

发布于 2024-11-12 22:55:15 字数 602 浏览 1 评论 0原文

我正在尝试测试一个由多个单词组成的字符串,并且其末尾是否有数组中的任何值。以下是我到目前为止所拥有的。我陷入了如何检查字符串是否比正在测试的数组值长以及它是否出现在字符串末尾的问题上。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓味的萝莉 2024-11-19 22:55:15

字符串结尾是指最后一个单词吗?你可以使用 preg_match

preg_match('~/?Wizard\??$~', $string, $matches);
echo "<pre>".print_r($matches, true)."</pre>";

By end of string do you mean the very last word? You could use preg_match

preg_match('~/?Wizard\??$~', $string, $matches);
echo "<pre>".print_r($matches, true)."</pre>";
压抑⊿情绪 2024-11-19 22:55:15

我认为你想要这样的东西:

if (preg_match('/\/?Wizard\??$/', $string)) { // ...

如果它必须是一个任意数组(而不是包含你在问题中提供的“向导”字符串的数组),你可以动态构造正则表达式:

$words = array('wizard', 'test');
foreach ($words as &$word) {
    $word = preg_quote($word, '/');
}
$regex = '/(' . implode('|', $words) . ')$/';
if (preg_match($regex, $string)) { // ends with 'wizard' or 'test'

I think you want something like this:

if (preg_match('/\/?Wizard\??$/', $string)) { // ...

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:

$words = array('wizard', 'test');
foreach ($words as &$word) {
    $word = preg_quote($word, '/');
}
$regex = '/(' . implode('|', $words) . ')$/';
if (preg_match($regex, $string)) { // ends with 'wizard' or 'test'
℉服软 2024-11-19 22:55:15

这是你想要的吗(不保证正确性,无法测试)?

foreach( $test_array as $testString ) {
  $searchLength = strlen( $testString );
  $sourceLength = strlen( $string );

  if( $sourceLength <= $searchLength && substr( $string, $sourceLength - $searchLength ) == $testString ) {
    // ...
  }
}

我想知道一些正则表达式在这里是否更有意义。

Is this what you want (no guarantee for correctness, couldn't test)?

foreach( $test_array as $testString ) {
  $searchLength = strlen( $testString );
  $sourceLength = strlen( $string );

  if( $sourceLength <= $searchLength && substr( $string, $sourceLength - $searchLength ) == $testString ) {
    // ...
  }
}

I wonder if some regular expression wouldn't make more sense here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文