skip to Main Content

I currently have a really great preg_replace system going on for my seo needs, but I’m having trouble with one character specifically and that’s the $. The code I have returns the $ as a blank but I would prefer it if it turned the $ to the letter S. It’s probably just a minor tweak but I’m sure how to single it out.

    $urlname = preg_replace("/[^a-zA-Z0-9/_|+ -]/", '', $info['name']);
    $urlname = strtolower(trim($urlname, '-'));
    $urlname = preg_replace("/[/_|+ -]+/", '-', $urlname);  
    $seourl = strtolower($urlname);

3

Answers


  1. Chosen as BEST ANSWER

    I was overthinking it. I should have changed it before I stripped out all the other special characters. Thanks, Siddhu Siddhartha Roy.

        $urlname = str_replace("$","s", $info['name']);
        $urlname = preg_replace("/[^a-zA-Z0-9/_|+ -]/", '', $urlname);
        $urlname = strtolower(trim($urlname, '-'));
        $urlname = preg_replace("/[/_|+ -]+/", '-', $urlname);  
        $seourl = strtolower($urlname);
    

  2. <?php
    echo str_replace("$","s","Hello $!");
    
    ?>
    
    Login or Signup to reply.
  3. you can use, strtr

    echo strtr('Hello. I have $ Please replace the $', '$', 'S');
    

    See Demo Here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search