Solved

Removing characters in php

Posted on 2007-03-26
4
233 Views
Last Modified: 2007-03-26
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.
0
Comment
Question by:thunder99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
TeRReF earned 250 total points
ID: 18792001
$s = 'domain.com';
list($sld, $tld) = explode('.', $s);
echo $sld.'<br>'.$tld;
0
 
LVL 27

Assisted Solution

by:Cornelia Yoder
Cornelia Yoder earned 250 total points
ID: 18792002
$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.
0
 

Author Comment

by:thunder99
ID: 18792116
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).
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 18792361
Good job, thunder99.  It's right to share the points for simultaneous correct answers.  Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question