gpt4 book ai didi

PHP,如何查找字符串中是否有某个单词?

转载 作者:行者123 更新时间:2023-11-29 10:57:36 25 4
gpt4 key购买 nike

我尝试过使用\/b[[:<:]] ,不知道为什么它们不起作用。和%仅当它位于字符串的最开头时才有效。

在我的类别表中,我有如下字符串:

Race car driver
Driving Instructor

在搜索框中,如果用户输入 'driv' (不区分大小写),我希望上面的两个字符串都出现。

//if a person arrives at this page other than doing a search then go to the main page
if(!isset($_POST['search'])) {
header("Location:mainpage.php");
}

$search_sql="SELECT * FROM category WHERE user_id = '$user_id' AND cat_name LIKE '[[:<:]]".$_POST['search']."%'";

$search_query=mysqli_query($con,$search_sql);
if(mysqli_num_rows($search_query)!=0) {
$search_rs=mysqli_fetch_assoc($search_query);
}

if(mysqli_num_rows($search_query)!=0) {

do { ?>
<p><?php echo $search_rs['cat_name']; ?></p>

etc... etc ...

最佳答案

你可以这样做:

".. cat_name LIKE '".$_POST['search']."%' or cat_name LIKE '% ".$_POST['search']."%'";

所以它要么是第一个单词,要么前面至少有一个空格。

关于PHP,如何查找字符串中是否有某个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com