【发布时间】:2014-02-15 00:27:09
【问题描述】:
我的问题是我想在给定的字符串中找到一个单词,并且每次单击按钮时该单词的背景变化都会增加,就像在任何编辑器中进行搜索一样。
<?php
function writeMsg()
{
$str = "my bird is funny, we fun are funny,they are funny they are fun";
$keyword =$_GET['search'];
$strarr=explode(" ",$str);
//print_r($strarr);
foreach ($strarr as $val)
{
//print_r($val);
if($val==$keyword)
{echo hi;
$copy_date = preg_replace("/$keyword\b/",'<b style=background-color:yellow;>'.$keyword.'</b>', $val);
print $copy_date;
}
else
{
echo $val;
} }
//}
?>
<html>
<form action="#" method="get">
Please enter word to search<input type="text" name="search"><br/>
<input type="button" value="NEXT" onclick="document.write('<?php 'writeMsg()';?>');" />
</form>
</html>
【问题讨论】:
-
php 在服务器端运行——也就是说,在 html 到达之前,所有的 php 处理都已经完成。因此,您需要使用 JavaScript 之类的东西来获得一个以您想要的方式与用户交互的函数。这对您来说是一个可以接受的替代方案吗(您专门询问 php……)
-
你有什么错误吗?
-
更好....使用
-
你想一键高亮所有单词吗?每次点击增加一个亮点;还是将突出显示移到下一个项目?
标签: php search word highlight using