note 108956 deleted from function.str-split by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 17:56:15 +0000 |
| Subject: | note 108956 deleted from function.str-split by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218665@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
A little one-liner that came in handy. I thought i'd share for those who want to split a string
into two parts by the given offset.
<?php
/**
* Split a string into two parts at offset.
*
* @param string $string
* @param integer $offset
* @return mixed array and bool(false) if offset is out of scope
*/
function str_osplit($string, $offset){
return isset($string[$offset]) ? array(substr($string, 0, $offset), substr($string, $offset)) :
false;
}
$str = "Split a string into two parts at offset";
var_dump(str_osplit($str, 12));
/**
* Output:
array(2) {
[0]=>
string(12) "Split a stri"
[1]=>
string(27) "ng into two parts at offset"
}
*/
?>