我有以下 PHP 搜索脚本。
当我搜索时1234567
,它匹配确切的短语,但我希望它只匹配最初的 4 个字符,即1234
.
解释:
我希望它计算最初的 4 个字符并显示与这些初始字符匹配的结果。例如,如果有人搜索,1234567
那么脚本应该计算最初的 4 个字符1234
,即显示结果。同样,如果有人搜索456789
,那么脚本应该计算最初的 4 个字符4567
,即显示结果。
///explode search term
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each)
{
//construct query
$x++;
if ($x==1)
$construct .= "keywords LIKE '%$search_each%'";
else
$construct .= " OR keywords LIKE '%$search_each%'";
}
//echo out construct
$construct = "SELECT * FROM numbers WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "No results found.";
{
echo "$foundnum results found.<p><hr size='1'>";
while ($runrows = mysql_fetch_assoc($run))
{
//get data
$title = $runrows['title'];
$desc = $runrows['description'];
$url = $runrows['url'];
echo "<b>$title</b>
<b>$desc</b>
<a href='$url'>$url</a><p>";