1

我正在使用一些现成的代码,它正在使用 implode 和 explode 函数为照片分配标签,标签用户输入。虽然它做得不对,就好像你尝试了一个两个词的标签,它正在拆分它。所以我用我发现的正则表达式替换了一个 preg_split 函数的 explode 函数,但是即使在http://php.fnlist.com/regexp/preg_split上测试函数和正则表达式表明它正确拆分了标签,在我的应用程序中它完全忽略任何两个词的标签。

我试图从“犯罪,爱情,神秘,犯罪剧,浪漫”等输入中获得格式良好的标签:“犯罪,爱情,神秘,犯罪剧,浪漫”,而我得到的是:“犯罪,爱情,神秘,浪漫” "

我给出了下面的代码。请帮忙!!

   <?php
class PhotoTagsController extends AppController {
var $name = 'PhotoTags';

var $uses = array('PhotoTag', 'Photo');

function edit($id = null)
{
 $this->authorize();

  if(!($photo = $this->Photo->findById($id)))
  {
    $this->flash('error', ucfirst(i18n::translate('photo not found')));
    $this->redirect('/');
  }
  else
  {
    $this->authorize($photo['Photo']['user_id']);

    $this->set('photo', $photo);

    if(empty($this->data))
    {
      $photo['Photo']['tags'] = array();
      foreach($photo['PhotoTag'] as $tag)
        $photo['Photo']['tags'][] = $tag['tag'];
      $photo['Photo']['tags'] = implode(',', $photo['Photo']['tags']);

      $this->data = $photo;
    }
    else
    { 

    // foreach(explode(',', $this->data['Photo']['tags']) as $tag)


   foreach(preg_split("/[s]*[,][ s]*/", $this->data['Photo']['tags']) as $tag)

      {
        $tag = strtolower(rtrim($tag));  //trims whitespace at end of tag
        if(!empty($tag))
        {
          $found = false;

          for($i = 0; $i < count($photo['PhotoTag']); $i++)
          {
            if(isset($photo['PhotoTag'][$i]) && $photo['PhotoTag'][$i]['tag'] == $tag)
            {
              $found = true;
              unset($photo['PhotoTag'][$i]);
              break;
            }
          }

          if(!$found)
          {
            $this->PhotoTag->create();
            $this->PhotoTag->save(array('PhotoTag' => array('photo_id' => $photo['Photo']['id'], 'tag' => $tag)));
          }
        }
      }

      foreach($photo['PhotoTag'] as $tag)
        $this->PhotoTag->delete($tag['id']);

      $this->flash('valid', ucfirst(i18n::translate('tags changed')));
      $this->redirect('/photos/show/' . $photo['User']['username'] . '/' . $photo['Photo']['id']);
    }
  }
}

function ajax_edit($id = null) {
 $this->authorize();

 if(!($photo = $this->Photo->findById($id)))
 {
   die();
 }
 else
 {
   $this->authorize($photo['Photo']['user_id']);

 // foreach(explode(',', $this->params['form']['value']) as $tag)

foreach(preg_split("/[s]*[,][ s]*/", $this->params['form']['value']) as $tag)

   {
     $tag = strtolower(rtrim($tag));
     if(!empty($tag))
     {
       $found = false;

       for($i = 0; $i < count($photo['PhotoTag']); $i++)
       {
         if(isset($photo['PhotoTag'][$i]) && $photo['PhotoTag'][$i]['tag'] == $tag)
         {
           $found = true;
           unset($photo['PhotoTag'][$i]);
           break;
         }
       }

       if(!$found)
       {
         $this->PhotoTag->create();
         $this->PhotoTag->save(array('PhotoTag' => array('photo_id' => $photo['Photo']['id'], 'tag' => $tag)));
       }
     }
   }

   foreach($photo['PhotoTag'] as $tag)
     $this->PhotoTag->delete($tag['id']);

    echo $this->params['form']['value'];

   die();
 }
}
}
?>
4

2 回答 2

0

将您的正则表达式更改preg_split为:

/(\s+)?,(\s+)?/

或者...

/\s*,\s*/
于 2011-07-04T15:15:44.727 回答
0

我认为您试图使事情复杂化。如果你想用逗号分割,只需使用更简单的 explode() 函数。然后,您可以使用 trim() 去除空白。

$parts = explode(',', $input_string);
foreach ($parts as $value) {
   $results[] = trim($value);
}
于 2011-07-04T15:57:03.690 回答