从表单中获取标记并将其拆分到数组中的脚本

时间:2012-01-13 00:26:30

标签: php

我有一个带有简单textarea的functions.php页面,如下所示:

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

在此表单上,我可以粘贴某些标签,如下所示: TAG1 TAG2 TAG3 TAG4 *另一个标签下的一个标签

在我的index.php页面上

$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);
}

我需要一种分割标签的方法。我试图通过它们之间的空间分割标签(就像在preg拆分函数中一样) - 但是作为标签列表,一个在另一个之下没有空格。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

在正则表达式中,空格用\ s来表示

$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);
}

答案 1 :(得分:2)

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

会被任意数量的空格分割(如换行符,制表符等)。我希望人们也使用逗号等,所以,你也可以决定拆分任何非字母字符。

答案 2 :(得分:0)

您可以将其拆分为换行符:

explode("\n", $tags);