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


Script that take the tags from a form and splits them inside an array

我有一个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>

在这个表单上,我可以粘贴某些标签,如下所示:标签1标签2标签3标签4*一个标签在另一个下

在我的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分割函数中一样),但作为一个标签列表,一个在另一个下面没有空格。

有什么想法吗?

Ty

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

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

在regex中,空格用一个'' 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);
}

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

explode("'n", $tags);