Best way to create / split a string into tags

In my php application, the user can enter tags (e.g. here when asking a question). I assume this will be a regular expression, and I used one - mb_split ('\ W +', $ text) - to separate with non-word characters.

But I want to allow users to enter characters like "-, _, +, #", etc., which are valid for the URL and are common.

Are there solutions for this, or could be best practices?

thanks.

+5
source share
8 answers

Use the explode () function and separate it with spaces or commas. Example:

$string = 'tag1 tag-2 tag#3';
$tags = explode(' ', $string); //Tags will be an array
+23
source

\s+.

+9

\s + () \W + ( -).

+3

, :

# 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);

, , , , , , .

+2

, , stackoverflow. "".

, , :

mb_split('\s+', $text)

:

mb_split('\W+', $text)

!

+2

smart_explode() :

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_exploding ( "" ).

+1

: , .

- .

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 ing". "", .

+1

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 regexp.

0

All Articles