Search code examples
phptitlerequest-uri

REQUEST_URI - possible to replace characters?


I'm currently using this to give me a title based on the current URL:

<?php $url = $_SERVER["REQUEST_URI"]; 
echo"$url"; 
$url = trim ( $url ,'/' ); ?>
<title>mysite.com - <?php echo $url; ?></title>

Many of my urls are formatted like this:

mysite.com - 177_183_45_999

Is it possible to replace the underscores with hyphens? to acheive:

mysite.com - 177-183-45-999

thanks


Solution

  • Use str_replace:

    str_replace("_", "-", $url)
    

    Example:

    <?php echo str_replace("_", "-", $url); ?>
    

    Or for a sexy oneliner:

    $url = str_replace("_", "-", trim($_SERVER["REQUEST_URI"], "/"));