具有 quote/apostrophe 个变体的 wordpress 查询

wordpress query with quote/apostrophe varrients

这是一个双重问题。我有一个 ajax 请求对重复的 post 标题进行轮询,但它被不同的 quote/apostrophes 及其变体抛出,当我知道有重复时 returning 否定.

我有一个 post 标题为:"Ben’s Big Fish" 即带有 apostrophe (’)

但是查询以下内容总是返回负值:

Ben's Big Fish (')
Ben’s Big Fish (’)
Bens Big Fish (no apos)

然而,查询 Big Fish returns 所有带有这些词的 posts 标题变体,包括 post 带有引号和 [=42] 的标题=]其中有 rophes。

以下是同样引起问题的主要角色:

Apostrophe          '   '
Open single quote   ‘   ‘ 
Close single quote  ’   ’
--- 
Quotation mark      "   "
Open double quotes  “   “ 
Close double quotes ”   ”

由于用户经常从 MS Word 文档等中提取文本,因此这些字符出现了很多。

在 js 端,我通过此函数传递 post 标题进行编码,然后通过 json 将其发送到我的 ajax 处理程序:

function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;').replace(/‘/g, '&lsquo;').replace(/’/g, '&rsquo;').replace(/“/g, '&ldquo;').replace(/”/g, '&rdquo;');
} 

在我的 php ajax 挂钩中,我按如下方式处理传入的 POST 查询:

global $wpdb;
// Grab details from inbound POST array & prepare for sql
$title = html_entity_decode($_POST['post_title']); //first un-encode
$post_id = $_POST['post_id'];

$sim_query = "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_title LIKE '%%%s%%' AND ID != '%d'";
$sim_results = $wpdb->get_results( $wpdb->prepare( $sim_query, $wpdb->esc_like($title), $post_id ) );
if ($sim_results)
{ // Send the results back as json }

所以我的问题是 a) 如何按预期获得对 return 明显重复项的查询 b) 并且可能相关,有一种方法可以有效地搜索字符串以查找所有变体 apostrophe 和引号字符的出现而无需多次查询?

问题的症结其实又回到了JS的原始编码上。使我们感到困惑的关键字符之一:&apos;,实际上并没有被 html_entity_decode 解码,即使设置了 ENT_QUOTES 标志。相反,它期望 &#039;.

所以最后我们的js看起来像:

function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;').replace(/‘/g, '&lsquo;').replace(/’/g, '&rsquo;').replace(/“/g, '&ldquo;').replace(/”/g, '&rdquo;');
} 

我们在 PHP 中解码:

 $title = html_entity_decode($_POST['post_title'], ENT_QUOTES,  'UTF-8' ); //first un-encode

同样重要的是要注意,SQL 会拒绝使用单引号和撇号。它要求它们是 escaped by doubling them like so: ''. Wordpress takes care of the escaping for us when we use its SQL escaping class $wpdb->prepare