Divide a string

DescriptionPHP snippet by - 06/07/10

This PHP string snippet will divide a string, this instance uses the : (colon) as the dividing characted but you can use whatever you want, even a space. The strpos() function finds the numerical position of the colon in the string and then the substr_replace() function removes everything after and including the colon. Use this snippet to obtain a subdomain from a url, first add a str_replace() to remove the http:// then run this script with a period instead of a colon.

Tags

$string_to_divide = "This is the part of the string to keep: This is the part to be removed";
$dividing_character = ':';
$pos = strpos($string_to_divide, $dividing_character);
$divided_string = substr_replace($string_to_divide, "", $pos);

echo "Divided String = $divided_string";
  • Share
Authored by: Adam J Nowak
http://hyperspatial.com

Comments and Feedback