Solved

Removing characters in php

Posted on 2007-03-26
4
227 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is best version of php to use 6 44
Mysql sync between 3-4 mysql db 4 23
PHP preg_replace code convert to Delphi 14 30
html input type 3 21
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 dynamically set the form action using jQuery.

943 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now