-2

Please i need to replace link with capital letter to small letter using wp function E.g let every text after /get/ be replace with small letter.

<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">

here is my function, I dont know where i am getting it wrong. however i am not good in wp. please correct me by posting the full correct code.

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');
  • This is your fourth version of this question in half as many days. If you have something to add, please edit the original question. – Jacob Peattie May 27 at 3:33
  • please kindly help me out if you know how to fix this please. – XATA May 27 at 3:55
1
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');

the above fixed the issue for me. another way is below.

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, '-');
}

FINALLY FIXED

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.