php - 如何使用 PHP 为 SEO 目的最好地解码字符串中的特殊字符

标签 php seo html-entities

我有一个 CMS 正在运行。我发现我的 CMS 用户正在输入来自 Word 等中的复制和粘贴操作的特殊字符。

所以在元描述内容属性中,我看到一个菱形代替了倾斜的单右引号。

我进入数据库并将单引号更改为

例如,我的 CMS 现在呈现 it’s

我尝试在渲染期间使用 PHP 的 htmlentities()、htmlspecialchars_decode() 和 html_entity_decode() 对字符串进行转换。

每个清理指令都简单地呈现it’s

我应该使用 PHP 函数来翻译吗?这有关系吗?也就是说,我可以在对 SEO 必不可少的元标记中包含 html 代码吗?

希望这是清楚的...感谢任何反馈。

最佳答案

这是我出于同样的原因放在一起的。如果您愿意在他们的帖子中保留 HTML,则可以删除 strip_tag() 行。

function convert_to_plaintext($message)
  {
    $message = strip_tags($message);

    //Quotes: Replace smart double quotes with straight double quotes.
    //ANSI version for use with 8-bit regex engines and the Windows code page 1252.
    preg_replace('[\x84\x93\x94]', '"', $message);

    //Quotes: Replace smart single quotes and apostrophes with straight single quotes.
    //ANSI version for use with 8-bit regex engines and the Windows code page 1252.
    preg_replace("[\x82\x91\x92]", "'", $message);

    //Quotes: Replace straight apostrophes with smart apostrophes
    preg_replace("/(\b'\b)/", "?", $message);

    //Quotes: Replace straight double quotes with smart double quotes.
    //ANSI version for use with 8-bit regex engines and the Windows code page 1252.
    preg_replace('/(\B"\b([^"\x84\x93\x94\r\n]+)\b"\B)/', '?\1?', $message);

    //Quotes: Replace straight double quotes with smart double quotes.
    //Unicode version for use with Unicode regex engines.
    //preg_replace('/(\B"\b([^"\u201C\u201D\u201E\u201F\u2033\u2036\r\n]+)\b"\B)/', '?\1?', $message);
    $message = str_replace(array('%u201C','%u201D','%u201E','%u201F','%u2033','%u2036'),'"',$message);

    //Quotes: Replace straight single quotes with smart single quotes.
    //Unicode version for use with Unicode regex engines.
    //preg_replace("/(\B'\b([^'\u2018\u2019\u201A\u201B\u2032\u2035\r\n]+)\b'\B)/", "?\1?", $message);
    $message = str_replace(array('%u2018','%u2019','%u201A','%u201B','%u2032','%u2035'),"'",$message);

    //Quotes: Replace straight single quotes with smart single quotes.
    //ANSI version for use with 8-bit regex engines and the Windows code page 1252.
    preg_replace("/(\B'\b([^'\x82\x91\x92\r\n]+)\b'\B)/", "?\1?", $message);

    $message = str_replace("\n\n\n","\n\n",$message);
    $message = str_replace("\n\n\n","\n\n",$message);
    $message = str_replace("\n\n\n","\n\n",$message);
    $message = str_replace("\n\n\n","\n\n",$message);
    $message = str_replace("\n\n\n","\n\n",$message);
    $message = str_replace("\n",'<br/>',$message);

    return $message;
  }

关于php - 如何使用 PHP 为 SEO 目的最好地解码字符串中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9839186/

相关文章:

php - 使用 Xampp 和 Ubuntu 安装 Drupal 时出现问题

按名称搜索时未列出 Android 应用程序,下载时未显示所需权限

SEO:从谷歌中删除旧餐厅

go - 如何通过 io.Reader 转换 HTML 实体

ruby-on-rails - Rails 中的 HTML 实体 link_to

html - 维度中 "x"的正确 HTML 实体是什么?

php - 如何使用 tcpdf 缩放图像?

javascript - 使用 Javascript 循环将 PHP 数据发送到 Modal

php - 使用原则 2 选择从一个父类(super class)扩展的多个子类的实体

javascript - 为什么下一个/脚本不适用于 google recaptcha?