Solved

Removing characters in php

Posted on 2007-03-26
4
231 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
  • 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:yodercm
yodercm 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:yodercm
ID: 18792361
Good job, thunder99.  It's right to share the points for simultaneous correct answers.  Thanks!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

830 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