note 108956 added to function.str-split
| From: | Anonymous | Date: | Fri, 08 Jun 2012 01:02:39 +0000 |
| Subject: | note 108956 added to function.str-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188453@lists.php.net to get a copy of this message | ||
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"
}
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 90.210.99.132
----
Manual Page -- http://www.php.net/manual/en/function.str-split.php
Edit -- https://master.php.net/note/edit/108956
Del: integrated -- https://master.php.net/note/delete/108956/integrated
Del: useless -- https://master.php.net/note/delete/108956/useless
Del: bad code -- https://master.php.net/note/delete/108956/bad+code
Del: spam -- https://master.php.net/note/delete/108956/spam
Del: non-english -- https://master.php.net/note/delete/108956/non-english
Del: in docs -- https://master.php.net/note/delete/108956/in+docs
Del: other reasons-- https://master.php.net/note/delete/108956
Reject -- https://master.php.net/note/reject/108956
Search -- https://master.php.net/manage/user-notes.php