preg_split()和内爆()新手请帮忙(尽量做好标记)



我使用的是一些现成的代码,它使用的是内爆&分解函数为照片分配标签,用户键入标签。不过,它做得不对,就好像你尝试了一个两个单词的标签,它在分裂它。因此,我用我找到的regex替换了preg_split函数的burst函数,但即使在测试函数&regex打开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();
 }
}
}
?>

preg_split正则表达式更改为:

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

或者。。。

/s*,s*/

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

$parts = explode(',', $input_string);
foreach ($parts as $value) {
   $results[] = trim($value);
}

相关内容

  • 没有找到相关文章

最新更新