【发布时间】:2011-07-07 13:48:05
【问题描述】:
我试图找出如何搜索字符串以查找给定变量,如果它长于两个字符并且存在于数组中,则返回该字符串并以某种方式突出显示搜索词。我已经尝试了两天,但它不正确,非常欢迎任何帮助!
我的 php:
$search = $_POST ['search'];
$text = 'some long text containg several words.';
$search = trim($search);
$search = strtolower($search);
$text = explode($text);
if ((in_array($search, $text)) && ($search >= 2)){
echo $text;
}else{
echo "no result or too short search word";
}
还有我的html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>word for which to search:</P>
<form action="searchfunction.php" method="post">
<input type="text" name="search" /><br />
<input type="submit" />
</form>
</body>
</html>
【问题讨论】:
-
所以现在,感谢 Michael,php 看起来像 Michaels 示例中的那样,但搜索结果只显示任何单词太短或不在数组中,即使我输入了一个我知道的单词数组...关于为什么的任何想法???
-
用
var_dump($_POST);检查脚本接收到的实际$_POST