• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Removing characters in php

I'm trying to echo out a list of domain names in php.  I need to print the sld and tld part separately (example sld=mydomain tld=com), how do I strip the text before and after the period using php?

BTW: Some domains have a 2 letter country code, so I can't just strip the last 4 characters.
  • 2
2 Solutions
$s = 'domain.com';
list($sld, $tld) = explode('.', $s);
echo $sld.'<br>'.$tld;
Cornelia YoderArtistCommented:
$array = explode(".",$url);
$firstpart = $array[0];
$lastpart = $array[1];

This only works for a two-part name like the example you gave.

If you need to break out abc.xyz.com into more parts, you do it the same way, but $array will have each part between the periods as a separate element in the array.  You can reassemble the parts however you need.
thunder99Author Commented:
That was quick.

Both answers work (I went with yodercm's though) and both have the same timestamp, so I'm going to accept multiple solutions.

Thanks guys (or gals).
Cornelia YoderArtistCommented:
Good job, thunder99.  It's right to share the points for simultaneous correct answers.  Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now