将特定文本替换为后面的小字段(_S)

时间:2020-05-26 作者:XATA

请我需要用大写字母替换链接到小写字母使用wp函数。g让每个文本在/get/ 替换为小写字母。

<a href="https://stackexchange.com/get/ASK-Question"> => <a href="https://stackexchange.com/get/ask-question">
<a href="https://stackexchange.com/get/aSK-QuesTion"> => <a href="https://stackexchange.com/get/ask-question">
这是我的函数,我不知道哪里出错了。然而,我不擅长wp。请通过发布完整的正确代码来更正我。

function emailleftappend($content){

        $findleft = \'/get\\/(?<=\\/)([A-Za-z]+?) ([A-Za-z]+?)(?=\\/">)/m\';
        $replaceleft = \'$1-$2\';
        $content = preg_replace(strtolower($findleft), $replaceleft, $content);

    return $content;
}
add_filter(\'the_content\', \'emailleftappend\');

1 个回复
最合适的回答,由SO网友:XATA 整理而成

function emailleftappend($content){


    $content = preg_replace_callback(\'/(?<=get\\/)(.*?)-(.*?)(?=\\/">)/\', function ($m) {
  return sanitize_title($m[1]). \'-\'. sanitize_title($m[2]); }, $content);

    return $content;
}
add_filter(\'the_content\', \'emailleftappend\');
以上为我解决了这个问题。另一种方法如下。

function emailleftappend($content){

        $content = preg_replace_callback(\'/(?<=get\\/)(.*?)-(.*?)(?=\\/">)/\', function ($m) {
      return slug($m[1]). \'-\'. slug($m[2]); }, $content);

        return $content;
    }
    add_filter(\'the_content\', \'emailleftappend\');

function slug($z){
    $z = strtolower($z);
    $z = preg_replace(\'/[^a-z0-9 -]+/\', \'\', $z);
    $z = str_replace(\' \', \'-\', $z);
    return trim($z, \'-\');
}
最终修复

相关推荐

Problem in functions.php file

我正在尝试使用functions.php. 以下是我迄今为止所做的工作: <?php function blogroom() { wp_enqueue_style(\'bootstrap\', get_stylesheet_directory_uri() . \'/assets/lib/bootstrap/dist/css/bootstrap.min.css\'); wp_enqueue_style(\'loade