dsxz84851 2011-07-04 15:00 采纳率: 100%
浏览 47

preg_split()和implode()新手帮忙请(尝试做正确的标签)

I am using some ready-made code and it was using implode & explode functions to assign tags to photos, tags users typed in. It was not doing it right though, as if you tried a two word tag, it was splitting it. So I replaced the explode function with a preg_split function with a regex I found, but even though testing the function & regex on http://php.fnlist.com/regexp/preg_split shows that it splits the tags correctly, in my application it totally ignores any two-word tags.

I am trying to get, from input like "crime, love, mystery ,crime drama,romance" nicely formatted tags: "crime,love,mystery,crime drama,romance" and I get instead: "crime, love, mystery,romance"

I am giving the code I have below. Please help!!

   <?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();
 }
}
}
?>
  • 写回答

2条回答 默认 最新

  • dragon88112 2011-07-04 15:15
    关注

    Change your preg_split regex to:

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

    Or...

    /\s*,\s*/
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法