0

I am using this php code

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

to grab the subdomain (subdomain.domain.co.uk) which works great However - I want it to ignore the hyphen and capitalise the words for hyphenated subdomains i.e. sub-domain.domain.co.uk => Sub Domain

What do I have to alter my code to?

2 Answers2

0

Use str_replace('-', ' ', $subdomain) before calling ucwords to replace - with a space. For example:

<?php
$subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
echo ucwords(str_replace('-', ' ', $subdomain));
?>
Divey
  • 1,699
  • 1
  • 12
  • 22
  • Just thought- one more thing - what would I have to change it to for the result to be 1) all lower case and without hyphens and 2) also all lower case and with hyphens? – Joseph Naylor Aug 24 '13 at 11:33
  • `strtolower` will convert a string to all lower case. Using the example above: 1) `echo strtolower(str_replace('-', ' ', $subdomain));` and 2) `echo strtolower($subdomain);` – Divey Aug 24 '13 at 12:05
  • I am getting this error now Strict standards: Only variables should be passed by reference in /home/bluesky1/public_html/middlesex/index.php on line 93 – Joseph Naylor Aug 25 '13 at 21:55
  • Assign `explode(".",$_SERVER['HTTP_HOST'])` to a variable and then pass that variable into `array_shift()`. See http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference. – Divey Aug 26 '13 at 16:01
0

Tried 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;
?>
bystwn22
  • 1,776
  • 1
  • 10
  • 9