代码之家  ›  专栏  ›  技术社区  ›  waney

创建/拆分字符串到标签的最佳方法

  •  5
  • waney  · 技术社区  · 17 年前

    在我的php应用程序中,用户可以输入标签(就像在这里提问一样)。

    但我想允许用户输入“-、_、+、#”等字符,这些字符在url中是有效的,也是常见的。

    谢谢。

    8 回复  |  直到 17 年前
        1
  •  23
  •   VirtuosiMedia    17 年前

    explode() 函数,并用空格或逗号分隔。例子:

    $string = 'tag1 tag-2 tag#3';
    $tags = explode(' ', $string); //Tags will be an array
    
        2
  •  9
  •   Zach Scrivena    17 年前

    按空格分隔 \s+ 相反。

        3
  •  3
  •   chaos    17 年前

        4
  •  2
  •   Michael Ekoka    17 年前

    # List characters that you would want to exclude from your tags and clean the string
    $exclude = array( '/[?&\/]/', '/\s+/');
    $replacements = array('', ' '); 
    $tags = preg_replace($exclude, $replacements,  $tags);
    
    # Now split:
    $tagsArray = explode(' ', $tags);
    

    您可能也可以采用白名单方法来解决这个问题,而是在模式中列出您接受的字符。

        5
  •  2
  •   Willem    17 年前

    你说你想让它像stackoverflow标记器一样工作。此标记器通过空格字符“”将它们分开。

    mb_split('\s+', $text)
    

    而不是:

    mb_split('\W+', $text)
    

    祝你好运

        6
  •  1
  •   Ilya Birman    17 年前

    function smart_explode ($exploder, $string, $sort = '') {
      if (trim ($string) != '') {
        $string = explode ($exploder, $string);
        foreach ($string as $i => $k) {
          $string[$i] = trim ($k);
          if ($k == '') unset ($string[$i]);
        }
        $u = array_unique ($string);
        if ('sort' == $sort) sort ($u);
        return $u;
      } else {
        return array ();
      }
    }
    

    它使用$exploder作为分隔符(通常是逗号)将$字符串分解为数组,删除重复项,修剪标签周围的空格,如果$sort是“sort”,甚至为您对标签进行排序。当$字符串中没有任何内容时,它将返回一个空数组。

    $mytaglist = smart_explode (',', '  PHP,  ,,regEx ,PHP');
    

    array ('PHP', 'regEx')
    

    要过滤你不喜欢的字符,请执行以下操作

     $mytaglist = str_replace (array ('?', '$', '%'), '_', $mytaglist);
    

    在smart_explosing之前(列出数组中的坏字符,用下划线替换)。

        7
  •  1
  •   Jukka Dahlbom    17 年前

    处理标签的正确方法取决于您对处理输入的偏好:您可以完全删除无效的标签,也可以尝试清理标签使其有效。

    在清理输入时,应使用定义有效字符的白名单方法——因为有太多的问题字符需要列入黑名单。

    mb_internal_encoding('utf8');
    
    $tags= 'to# do!"¤ fix-this str&ing';
    $allowedLetters='\w';
    // Note that the hyphen must be first or last in a character class pattern,
    // to match hyphens, instead of specifying a character set range
    $allowedSpecials='_+#-';
    

    // The first way: Ignoring invalid tags
    
    $tagArray = mb_split(' ', $tags);
    
    $pattern = '^[' . $allowedLetters . $allowedSpecials . ']+$';
    
    $validTags = array();
    foreach($tagArray as $tag)
    {
        $tag = trim($tag);
        $isValid = mb_ereg_match($pattern, $tag);
        if ($isValid)
            $validTags[] = $tag;
    }
    

    // The second way: Cleaning up the tag input
    
    // Remove non-whitelisted characters
    $pattern = '[^' . $allowedLetters . $allowedSpecials .']';
    
    $cleanTags = mb_ereg_replace($pattern, ' ', $tags);
    
    // Trim multiple white spaces.
    $pattern = '\s+';
    $cleanTags = mb_ereg_replace($pattern, ' ', $cleanTags);
    
    $tags = mb_split(' ',$cleanTags);
    

    用空格替换非法字符会导致问题 有时,例如上面的“str&ing”会被转换为“str”。 完全删除非法字符将导致“string” 在某些情况下更有用。

        8
  •  0
  •   vartec    17 年前

    使用 preg_match_all .

    $tags = array();
    if(preg_match_all('/\s*(.*)\s*/U',$tags)) unset($tags[0]);
    //now in $tags you have an array of tags. 
    

    如果标签是UTF-8,请添加 u 正则表达式的修饰符。

    推荐文章