我的问题是,我希望在每次点击按钮时找到给定字符串中的单词和该单词的背景更改,并且计数器会像在任何编辑器中的下一个搜索一样增加。
<?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>
答案 0 :(得分:0)
如果你没有使用正则表达式,str_replace()
就可以了。它甚至还有一个内置的柜台。
<?php
function writeMsg(){
$count = 0;
$str = "my bird is funny, we fun are funny, they are funny they are fun";
$keyword = $_GET['search'];
$str = str_replace($keyword, "<b style='background-color:yellow;'>".$keyword."</b>",$str,$count);
echo "There were ".$count." matches.<br />";
echo $str;
}
?>
<html>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
Please enter word to search: <input type="text" name="search"><br/>
<input type="submit" value="NEXT" />
</form>
<?php
if(isset($_GET['search'])){
writeMsg();
}
?>
</html>
答案 1 :(得分:0)
您只需使用str_replace
这样的功能即可完成此操作。
<?php
$str = "my bird is funny, we fun are funny,they are funny they are fun";
$str1=$str;
echo $str."<br/>";
$find="funny";
$str=str_replace($find,'<span style="color:red">'.$find.'</span>',$str);
echo $str."<br/>"; //If keyword exists displays updated string
$find="funky";
$str=str_replace($find,'<span style="color:red">'.$find.'</span>',$str1);
echo $str; //If keyword doesn't exist, Just display the same string
?>
答案 2 :(得分:0)
简易Javascript实施:
String: <input type = "text" id = "mystring">
<p> </p>
<div id = "mystringdiv" style="width:400px; height:120px; border:solid black 1px;"></div>
Word to search: <input type = "text" id = "searchstring"><input type = "button" value = "Search" onclick = "startSearch()">
<script type = "text/javascript">
function startSearch()
{
var search = new RegExp(document.getElementById("searchstring").value, "g")
var string = document.getElementById("mystring").value
var nstring = string.replace(search, "<b style=\"background-Color:#FFFF00\">"+document.getElementById("searchstring").value+"</b>")
document.getElementById("mystringdiv").innerHTML=nstring
}
</script>
答案 3 :(得分:0)
到目前为止,所有答案都无法解决在链接中使用关键字时会发生什么情况。
这是我的解决方案。它可能并不完美,但它允许这样做。
//this code will highlight the search string in the returned search results. It ignores text within tags
function doHighlight ($strneedle, $strhaystack){
//this fixes bug if first char is a <
if(strpos($strhaystack,"<")==0) {$strhaystack = " " . $strhaystack;}
$strhaystack = str_replace("\"","'",$strhaystack);
$newthought = "";
$i = 0;
//only do it if we have a tag
$findtag = strpos($strhaystack,"<");
if ($findtag > 0){
//count how many tags we have
$strCount = substr_count( $strhaystack,"<");
//and loop that many times
while($i < $strCount){
$i++;
//find opening tag
$pos = strpos( $strhaystack,"<");
//get text to left of opening tag
$thoughtstart = substr($strhaystack , 0, $pos);
//highlight that text
$pattern = $strneedle;
$replacement = '<span class="highlight">'.$strneedle.'</span>';
$highlightedthought = eregi_replace($pattern, $replacement, $thoughtstart);
//build the new string
$newthought = $newthought . $highlightedthought;
//find closing tag and concatenate it to new string without highlighting
$thoughtremainder = substr($strhaystack, $pos);
$pos = strpos( $thoughtremainder,">");
$thoughtstart = substr($thoughtremainder , 0, $pos);
$newthought = $newthought . $thoughtstart;
//make the rest of the thought the new string and loop
$strhaystack = substr($thoughtremainder, $pos);
}
}
//if no tag, return the highlighted thought
else{
$pattern = $strneedle;
$replacement = '<span class="highlight">'.$strneedle.'</span>';
$newthought = eregi_replace($pattern, $replacement, $strhaystack);
}
return (trim($newthought));
}