【问题标题】:Using ucwords and explode to ignore hyphens and capitalise words in php在php中使用ucwords和explode忽略连字符和大写单词
【发布时间】:2013-08-24 10:15:44
【问题描述】:

我正在使用这个 php 代码

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

获取效果很好的子域 (subdomain.domain.co.uk) 然而 - 我希望它忽略连字符并将连字符子域的单词大写 即 sub-domain.domain.co.uk => 子域

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

【问题讨论】:

    标签: php echo explode subdomain wildcard-subdomain


    【解决方案1】:

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

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

    【讨论】:

    • 只是想-还有一件事-我必须将其更改为结果为 1) 全部小写且不带连字符和 2) 也全部小写且带连字符?
    • strtolower 会将字符串转换为全部小写。使用上面的示例:1)echo strtolower(str_replace('-', ' ', $subdomain)); 和 2)echo strtolower($subdomain);
    • 我现在收到此错误严格标准:只有变量应该在第 93 行的 /home/bluesky1/public_html/middlesex/index.php 中通过引用传递
    • explode(".",$_SERVER['HTTP_HOST']) 分配给一个变量,然后将该变量传递给array_shift()。见stackoverflow.com/questions/2354609/…
    【解决方案2】:

    试过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;
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 2011-06-03
      相关资源
      最近更新 更多