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

发布于 2024-11-18 04:23:09 字数 3397 浏览 3 评论 0原文

我正在使用一些现成的代码,它使用的是 implode &爆炸功能可以为照片分配标签,用户输入的标签。 但它做得不对,就像你尝试使用两个单词的标签一样,它正在将其分开。 因此,我用我发现的正则表达式的 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();
 }
}
}
?>

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();
 }
}
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-11-25 04:23:10

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

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

或者...

/\s*,\s*/

Change your preg_split regex to:

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

Or...

/\s*,\s*/
书信已泛黄 2024-11-25 04:23:10

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

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

I think you're trying to over complicate things. If you want to split by comma's, just use the more simple explode() function. You can then use trim() to strip off the white space.

$parts = explode(',', $input_string);
foreach ($parts as $value) {
   $results[] = trim($value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文