Php 用条令删除特殊字符
我在数据库中搜索城市,当我键入“Aulnay sous bois”时,没问题,我可以找到有问题的城市,但当我键入“Aulnay sous bois”时,它不起作用 我已经尝试删除字符,但它不起作用 我的代码控制器:Php 用条令删除特殊字符,php,sql,symfony,doctrine,Php,Sql,Symfony,Doctrine,我在数据库中搜索城市,当我键入“Aulnay sous bois”时,没问题,我可以找到有问题的城市,但当我键入“Aulnay sous bois”时,它不起作用 我已经尝试删除字符,但它不起作用 我的代码控制器: public function verifyCity(Request $request, CityRepository $cityRepository): JsonResponse { if($request->isXmlHttpRequest()) {
public function verifyCity(Request $request, CityRepository $cityRepository): JsonResponse
{
if($request->isXmlHttpRequest()) {
$data = [];
$searchs = $cityRepository->findBySearchCity($request->request->get('city'));
/**
* @var $searchs City[]
*/
foreach ($searchs as $search) {
$data[] = [
'name' => $search->getName(),
'zipCode' => $search->getZipCode()
];
}
return new JsonResponse(['city' => $data, 'ok' => 1]);
}
return new JsonResponse(['error' => 'not found']);
}
我的存储库
public function findBySearchCity($search,$zipCode = null)
{ //SELECT * FROM `city` WHERE name LIKE "%la courn%" OR zip_code LIKE "78%"
$query =$this->createQueryBuilder("c")
->where('c.name LIKE :search')
->setParameter('search', "%".$search."%");
if($zipCode != null) {
$query->orWhere('c.zipCode LIKE :zipCode')
->setParameter('zipCode', $zipCode."%");
}
return $query->getQuery()->setMaxResults(5)
->getResult();
}
和Js:
var cityTabs = [];
$('#registration_form_city').keyup(function () {
var city = this.value;
var data = {
city: city,
};
$.ajax({
url: $('#wizardProperty').attr('data-verify-city'),
type: 'POST',
data: data,
dataType: 'JSON',
success: function (data) {
cityTabs = [];
if(data.ok == 1) {
$.each(data.city,function (k, v) {
cityTabs.push(v.name);
});
}
console.log(cityTabs);
$( "#registration_form_city" ).autocomplete({
source: cityTabs
});
},
error: function (data) {
console.log(data)
}
});
});
如上所述,当我完全使用破折号输入时,它可以工作,但当我不使用破折号输入时,它不再工作,如何在删除“-”的同时显示结果您可以修改存储库方法中的
$search
参数值,使用preg\u split()将破折号和空格替换为%
通配符
和内爆()
因此,您的术语“Aulnay sous bois”或“Aulnay sous bois”都将导致使用以下语句进行查询:
。。。像“%Aulnay%sous%bois%”
一样,我将使用函数为您提供不同的替代方案。让我们开始准备表格:
ALTER TABLE city ADD FULLTEXT (`name`);
更正Arleigh的回答,查询应该是这样的:
public function findBySearchCity($search, $zipCode=null) {
// Can be done in one line with preg_replace()
// Replace underscore, any white-space and any non-word character with '%'
$search=preg_replace("/[_\W\s]+/", '%', $search);
// Always split init from the rest of the query in case you need to use `$qb->expr()`
$qb=$this->createQueryBuilder("city");
if($zipCode !== null) {
$qb->andWhere($qb->expr()->orX(
$qb->expr()->like("city.name", ":search"),
$qb->expr()->like("city.zipCode", ":zipCode")))
->setParameter('zipCode', $zipCode.'%');
} else {
$qb->andWhere($qb->expr()->like("city.name", ":search"));
}
$qb->setParameter('search', '%'.$search.'%')
->setMaxResults(5);
return $qb->getQuery()->getResult();
}
它应该是这样工作的。
如果没有,则转储查询,用它更新您的问题,并在此处进行评论
提示:
- 查询时始终使用双引号。条令只接受字符串值的单引号(参数可以使用单引号)
- 切勿使用
where()
,始终以和where()
或或where()开头
- 避免同时使用
和where()
和或where()
。首选expr()
()
- 避免将DQL与伪本机(如
,中的等)SQL混合使用。如果条令读到一些不能使用的本地SQL,它会咬你。改用expr()
。如果在expr()
中找不到所需内容,则意味着您应该创建一个本机查询
您好,谢谢您的回复,我试过了,但没有用!
public function findBySearchCity($search, $zipCode=null) {
// Can be done in one line with preg_replace()
// Replace underscore, any white-space and any non-word character with '%'
$search=preg_replace("/[_\W\s]+/", '%', $search);
// Always split init from the rest of the query in case you need to use `$qb->expr()`
$qb=$this->createQueryBuilder("city");
if($zipCode !== null) {
$qb->andWhere($qb->expr()->orX(
$qb->expr()->like("city.name", ":search"),
$qb->expr()->like("city.zipCode", ":zipCode")))
->setParameter('zipCode', $zipCode.'%');
} else {
$qb->andWhere($qb->expr()->like("city.name", ":search"));
}
$qb->setParameter('search', '%'.$search.'%')
->setMaxResults(5);
return $qb->getQuery()->getResult();
}