Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to create/split string to tags

Tags:

regex

php

In my php application user able to enter the tags(like here while ask question). I assume it will be regexp, and I used one - mb_split('\W+', $text) - to split by non-word characters.

But I want to allow users to enter characters like "-,_,+,#" etc which are valid ones to be in url and are common.

Is there exiting solutions for this, or may be best practicles?

thanks.

like image 607
waney Avatar asked Feb 10 '09 00:02

waney


People also ask

How do you split a string in HTML?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Can we use split in HTML?

I use split(" ") for whitespace and use split(/(<[^>]*>)/) for html tag in string.

What is splitting in PHP?

Definition and Usage. The str_split() function splits a string into an array.


3 Answers

Use the explode() function and separate by either spaces or commas. Example:

$string = 'tag1 tag-2 tag#3';
$tags = explode(' ', $string); //Tags will be an array
like image 191
VirtuosiMedia Avatar answered Oct 04 '22 20:10

VirtuosiMedia


Split by whitespace \s+ instead.

like image 34
Zach Scrivena Avatar answered Oct 04 '22 20:10

Zach Scrivena


Split on \s+ (whitespace) instead of \W+ (non-alphanumeric).

like image 39
chaos Avatar answered Oct 04 '22 20:10

chaos