1

私はいくつかの既製のコードを使用しており、内破と爆発機能を使用して写真にタグを割り当て、ユーザーが入力したタグを付けていました。だから私は爆発関数を私が見つけた正規表現でpreg_split関数に置き換えましたが、http://php.fnlist.com/regexp/preg_splitで関数と正規表現をテストしてもタグが正しく分割されることが示されていますが、私のアプリケーションでは2 語のタグは完全に無視されます。

「犯罪、愛、ミステリー、犯罪ドラマ、ロマンス」のような入力から、適切にフォーマットされたタグ「犯罪、愛、ミステリー、犯罪ドラマ、ロマンス」を取得しようとしていますが、代わりに「犯罪、愛、ミステリー、ロマンス」を取得します"

以下にコードを示します。助けてください!!

   <?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 に答える