我有这个脚本:
<?php
$string = "@Erol Simsir @Hakan Simsir";
$output = preg_replace("/@([^\s]+)/", "<a href=\"?tag=$1\">$1</a>", $string);
echo $output;
?>
此脚本会检测前面带有“@”符号的所有单词,并将其更改为链接/标记。但是,我不想要一个标签系统,而是一个标签系统,比如在Twitter上,你可以在那里做'@JohnDoe'而用户JohnDoe就是Tweet去的人。
因此,我需要的是将所有标记存储在数组中的字符串中,以便以后将它们用于SQL查询。
我怎样才能做到这一点?
更新
我现在有这个代码:
$string = $_POST["tags"];
$output = preg_replace("/@([^\s]+)/", "<a href=\"?tag=$1\">$1</a>", $string);
$finalOutput = explode(" ", $string);
$count = count($finalOutput);
$i = 0;
while($i < $count) {
echo $finalOutput[$i];
$i = $i + 1;
}
问题是,输出中的标签如下所示:@john @sandra等。如何删除输出中的@符号?
答案 0 :(得分:1)
$array = explode(' ', $string);
尝试使用此代码:)
$count = count($array);
$tag_array = array();
$j=0;
for($i=0;$i<$count;$i++)
{
if(substr($array[$i],0,1)==='@')
{
$tag= ltrim ($array[$i],'@');
$tag_array[$j] = $tag;
$j++;
}
}
print_r($tag_array);
如果您需要任何进一步的帮助,请告诉我们。)
答案 1 :(得分:1)
最简单的方法是使用preg_match_all:
$string = "@Erol Simsir @Hakan Simsir";
preg_match_all("/@(\w+)/", $string, $output);
print_r($output[0]);
答案 2 :(得分:0)
我看到这个脚本出了问题:
$string = $_POST["tags"];
$output = preg_replace("/#([^\s]+)/", "<a href=\"?tag=$1\">$1</a>", $string);
$finalOutput = explode(" ", $output);
$count = count($finalOutput);
$i = 0;
while($i < $count) {
echo $finalOutput[$i] . "<br />";
$i = $i + 1;
}
它还会在没有@符号的情况下爆炸所有单词。当我只是在表单中插入'JohnDoe'时,它也接受它,但是它应该只接受带有@符号的文本。因此,当我在脚本中插入“JohnDoe”时,它应该验证并说这不是有效标记。只有当输入像“@john @steve”等那样它才能起作用。