这听起来可能很愚蠢,我试图在我的结果中突出显示搜索到的字母/单词并找到了下面的函数,但不知道我应该把它放在哪里,我已经尝试在下面的 php 中的标签和上面的标签下,但没有运气
function sublinhamos($text,$searchquery) {
$wordsArray = array();
$markedWords = array();
// explode the phrase in words
$wordsArray = explode(' ', $searchquery);
foreach ($wordsArray as $k => $searchquery) {
$markedWords[$k]='<mark>'.$searchquery.'</mark>';
}
$text = str_ireplace($wordsArray, $markedWords, $text);
//right trows results
return $text;
}
致命错误:使用下面的代码在第 20 行的 /home/u472061620/public_html/search4.php 中调用未定义的函数 sublinhamos() 而不插入上面的代码。
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
if($_POST['filter1'] == "Whole Site"){
$sqlCommand = "(SELECT * FROM products WHERE product_name LIKE '%$searchquery%' OR details LIKE '%$searchquery%') ";
}
require_once("storescripts/connect_to_mysqli.php");
$query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
$count = mysqli_num_rows($query);
if($count >= 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
while($row = mysqli_fetch_array($query)){
$id=$row["id"];
$product_name = sublinhamos($row["product_name"],$searchquery);
$details = sublinhamos($row['details'],$searchquery);
$category=$row["category"];
$subcategory=$row["subcategory"];
$search_output .= "ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
<a href='product.php?id=$id'>link</a><br/>";
} // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
谁能告诉我应该把函数 sublinhamos 放在哪里?在标签内尝试,上面有/没有标签的标签,在下面的php标签内......都没有运气
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function myTruncate($string, $limit, $break=" ", $pad="...")
{
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;
// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
if($_POST['filter1'] == "Whole Site"){
$sqlCommand = "(SELECT * FROM products WHERE product_name LIKE '%$searchquery%' OR details LIKE '%$searchquery%') ";
}
require_once("storescripts/connect_to_mysqli.php");
$query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
$count = mysqli_num_rows($query);
if($count >= 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
while($row = mysqli_fetch_array($query)){
$id=$row["id"];
$product_name =$row["product_name"];
$details = $row["details"];
$category=$row["category"];
$subcategory=$row["subcategory"];
$description = "<a href='product.php?id=$id'>ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
link</a><br/>";
$search_output = myTruncate($description, 100," ");
} // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
不用担心,我已经实现了上面的截断功能,因此似乎没有必要突出显示搜索的关键字..但是,如果可能的话,实现这个突出显示功能将是一个额外的功能。感谢那些试图提供帮助的人