0
votes

I have a functions.php page with a simple textarea, like this:

<tr valign="top">
<th scope="row">Tags:</th>
<td><textarea name="tags" id="tags" cols="40" rows="6"><? echo get_option('tags'); ?></textarea></td>
</tr>

On this form I can paste certain tags like this: tag1 tag2 tag3 tag4 *one tag under another

On my index.php page

$tags = get_option('tags');  // I pull all my submitted tags inside the $tags variable
if ($tags != ''){
    $tag = preg_split('/ /', $tags); // The problem is here
    $tag = array_map('trim', $tag);
}

I need a way to split the tags. I am trying to split the tags by the space between them (like in the preg split function) - but being a list of tags, one under the other there are no spaces.

Any ideas?

Ty

3

3 Answers

2
votes
preg_split('/\s+/',$tags);

would split by any amount of whitespace (so newlines, tabs, etc.). I would expect people to also use comma's and the like, so, you could also decide to split on any non-letter character for instance.

2
votes

in regex the space is represented with a \s try this

$tags = get_option('tags');  // I pull all my submitted tags inside the $tags variable
if ($tags != ''){
    $tag = preg_split('/\s+/', $tags); // The problem is here
    $tag = array_map('trim', $tag);
}
0
votes

You could split it on the newline character instead:

explode("\n", $tags);