PHP搜索前/后获取文本(PHP Search Get Text Before / After)

编程入门 行业动态 更新时间:2024-10-28 02:22:41
PHP搜索前/后获取文本(PHP Search Get Text Before / After)

我将如何将搜索结果转换为以下内容: http : //i.stack.imgur.com/NfPGs.png

结果显示特定术语在单元格中的位置。

我目前有这个基本的搜索脚本:

$terms = explode("-", $SQuery); $QuerySQL = "SELECT * FROM pages WHERE "; foreach ($terms as $each){ $i++; if ($i == 1) $QuerySQL .= "Title LIKE '%$each%' OR Content LIKE '%$each%' OR Description LIKE '%$each%'"; else $QuerySQL .= "OR Title LIKE '%$each%' OR Description LIKE '%$each%' OR Content LIKE '%$each%'"; } $QueryNEW = mysql_query($QuerySQL); WHILE($datarows_cat = mysql_fetch_array($QueryNEW)): $title = $datarows_cat['Title']; $Deleted = $datarows_cat['Deleted']; $id = $datarows_cat['ID']; if ($Deleted != "YES") { echo "<a href='/{$id}'>{$title}</a><br/>"; }

How would I go about turning my search results into something like this: http://i.stack.imgur.com/NfPGs.png

Where the results show where in the cell the particular term is.

I currently have this basic search script:

$terms = explode("-", $SQuery); $QuerySQL = "SELECT * FROM pages WHERE "; foreach ($terms as $each){ $i++; if ($i == 1) $QuerySQL .= "Title LIKE '%$each%' OR Content LIKE '%$each%' OR Description LIKE '%$each%'"; else $QuerySQL .= "OR Title LIKE '%$each%' OR Description LIKE '%$each%' OR Content LIKE '%$each%'"; } $QueryNEW = mysql_query($QuerySQL); WHILE($datarows_cat = mysql_fetch_array($QueryNEW)): $title = $datarows_cat['Title']; $Deleted = $datarows_cat['Deleted']; $id = $datarows_cat['ID']; if ($Deleted != "YES") { echo "<a href='/{$id}'>{$title}</a><br/>"; }

最满意答案

您可以使用PHP strpos查找+/- 100个字符:

$pos = strpos($mystring, $findme); if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; }

然后使用PHP substr来获取$pos+100和$pos-100的子字符串

//For instance your substring could start at the position of the word found minus 100 characters and a length of 200 $result = substr($mystring, $pos-100, 200);

您可以使用PHP str_replace格式化您需要的字符串:

$word_your_looking_for = "test"; $word_you_want_to_replace_it_with = "<b>test</b>"; //Make it bold str_replace($word_your_looking_for, $word_you_want_to_replace_it_with, $your_string);

You can use PHP strpos to find +/- 100 characters:

$pos = strpos($mystring, $findme); if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; }

Then use PHP substr to get the substring of $pos+100 and $pos-100

//For instance your substring could start at the position of the word found minus 100 characters and a length of 200 $result = substr($mystring, $pos-100, 200);

You can use PHP str_replace to format the string you need:

$word_your_looking_for = "test"; $word_you_want_to_replace_it_with = "<b>test</b>"; //Make it bold str_replace($word_your_looking_for, $word_you_want_to_replace_it_with, $your_string);

更多推荐

本文发布于:2023-04-28 09:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   PHP   Text   Search

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!