Splitting String

Splitting String

The following example is used to split a string into two parts by providing it the index that the split should occur.

<?php

function str_split_index_fn($string, $index)

{

$string = (string) $string;

$index = (int) $index;

$prefix = substr($string, 0, $index);

$suffix = substr($string, $index);

return array($prefix, $suffix);

}

$input = ‘Good Day!’;

$output= str_split_index_fn($input, 4);

print_r($output);

?>
In above example, “Good Day!” is the input which can be split into 2 parts. str_split_index_fn is user defined function, which will split the given text into 2 parts with the passed index.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.