0

好的,

所以这是我的代码:

        $explode = explode("+ ", $article);
        $explode_count = count($explode);

        for($i=0;$i<$explode_count;$i++)
        {
            $numbers = preg_replace('/[^0-9]/', '', $explode[$i]);
            $letters = preg_replace('/[^a-zA-Z]/', ' ', $explode[$i]);  

            if($letters == "All Star Game")
            {
                 echo "Done";
            } 
        }

$letters 变量等于 All Star Game。但是,出于某种原因,“完成”没有得到回应。可能是我用来将数字与字符串中的字母分开的 preg_replace 函数吗?我注意到当我将 $letters 变量更改为

   $letters = preg_replace('/[^a-zA-Z]/', '', $explode[$i]);    

并将其余代码设置为:

if($letters == "AllStarGame")
{
     echo "Done";
}

然后,php 将其呼应出来。这里的空间是怎么回事?

4

2 回答 2

1
于 2012-06-02T11:31:46.797 回答
0

It's because your regexp excludes spaces.

$letters = preg_replace('/[^a-zA-Z]/', ' ', $explode[$i]);
//                          ^ I only accept letters, not spaces

The solution be to forget regexp and explode by ' + '.

<?php
$teams = explode(' + ', $article);

foreach ($teams as $team) {
    if ($team == "All Star Game") {
         echo "Done";
    } 
}

Be warned that you may have trailling empty string at end of the array (your string contains ' + ' at end, but you can easily break when seeing empty string.

于 2012-06-02T11:31:41.017 回答