Inside-Out - Web Design & Development 4 Beginners

Splitting a string into shorter strings couldn’t be easier. Although there is a PHP function to do this – split(’delimiter‘, $string), this method is now antiquated and not recommended. A better method would be to use the explode() function.

<?php
$my_string = “Joe John Bloggs”;
$my_array = explode(” “, $my_string); // Explode string into array. Delimit via spaces.

// Now to retrieve our segments
echo “Your first name is “.$my_array[0].”, your middle name is “.$my_array[1].” and your surname “.$my_array[3].”

// You can also limit the amount of splits
$my_address = “Name, Address, City, Postcode, Country”;
$my_address_array = explode(” , “, $my_address, 2); // Explode string into array. Limit by array[2].

// This would give you an array like this:
/*
Array
(
    [0] => Name
    [1] => Address
    [2] => City, Postcode, Country
)
*/
?>

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • BlinkList
  • De.lirio.us
  • Furl
  • Reddit
  • StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Spam protection by WP Captcha-Free