在php中使用ucwords和explose来忽略连字符和大写单词


Using ucwords and explode to ignore hyphens and capitalise words in php

我使用的是这个php代码

    <title>Web Design <?php
    echo ucwords(array_shift(explode(".",$_SERVER['HTTP_HOST'])));
    ?>, Website Design</title>

获取子域(subdomain.domain.co.uk),这非常有效然而-我希望它忽略连字符,并将连字符子域的单词大写即子域.domain.co.uk=>子域

我必须将代码更改为什么?

在调用ucwords之前使用str_replace('-', ' ', $subdomain),用空格替换-。例如:

<?php
$subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
echo ucwords(str_replace('-', ' ', $subdomain));
?>

是否尝试过str_replace

<?php
  $domain = $_SERVER["HTTP_HOST"];
  $domain = explode( ".", $domain ); // split domain by comma
  $domain = array_shift( $domain ); // shift an element off the begginning of array
  $domain = str_replace( "-", " ", $domain ); // replace all occureance of '-' to space
  $domain = ucwords( $domain ); // uppercase the first character of words
  echo $domain;
?>