<?php $testString = "FirstName,MiddleName,LastName"; $testArray = split(",",$testString); print_r($testArray); //Array ( [0] => FirstName [1] => MiddleName [2] => LastName ) ?> <?php $testString = "FirstName MiddleName,LastName"; list($first, $middle, $last) = split('[ ,]', $testString); echo "First Name: $first; MiddleName: $middle; LastName: $last"; ?>
Tag Archives: String functions
Find out the first occurrence of a string – PHP
<?php $email = 'firstname@mail.com'; $domain = strstr($email, '@'); echo $domain; // prints@mail.com ?>
Function to find out the substring of a string – PHP
<?php echo substr('abcdef', 1, 3); // bcd ?>
Remove spaces from the end of a string – PHP
<?php echo rtrim("Test "); // Test //Will remove the spaces from the end ?>
Remove spaces from the beginning of a string – PHP
<?php echo ltrim(" Test"); // Test //Will remove the spaces from the beginning ?>
Remove the first and last item of the array – PHP
<?php $sampleData = array('10', '20', '30.30', '40', '50'); array_shift($sampleData); array_pop($sampleData); print_r($sampleData); // Array ( [0] => 20 [1] => 30.30 [2] => 40 ) ?>
Remove the first element of an array – PHP
<?php $sampleDate = array("Test1", "Test2", "Test3", "Test4"); array_shift($sampleDate); print_r($sampleDate); // Array ( [0] => Test2 [1] => Test3 [2] => Test4 ) . // Test1 will be shifted ?>
Remove the last element of an array – PHP
<?php $sampleDate = array("Test1", "Test2", "Test3", "Test4"); array_pop($sampleDate); print_r($sampleDate); // Array ( [0] => Test1 [1] => Test2 [2] => Test3 ) . // Test4 will be removed ?>
Replace string with another string – PHP
<?php $name = "Hello Name"; $name =str_replace("Name", "First Name", $name); echo $name; // Hello First Name ?>
Convert the first character of each word in a string into uppercase – PHP
<?php echo ucwords("hello name"); // Hello Name ?>