Split string into equal parts using PHP
str_split
was designed for just that.
$str = "sdasdasdsdkjsdkdjbskdbvksdbfksjdbfkdbfksdjbf";
$parts = str_split($str, 6);
print_r($parts);
Split two strings in equal parts and add a word in between
You can use array_splice. An example below:
$string = "hello i am superman and also batman";
$insert = "word";
$string_array = explode(' ',$string);
array_splice( $string_array, round(count($string_array)/2), 0, array($insert) );
echo implode(' ', $string_array);
Or use it as a function:
function insertString($string, $insert){
$string_array = explode(' ',$string);
array_splice( $string_array, round(count($string_array)/2), 0, array($insert) );
return implode(' ', $string_array);
}
echo insertString('hello i am superman and also batman','word');
Output will be:
hello i am superman word and also batman
Split array into equal parts order by id using PHP
Sounds like you wanted something like this:
$array = [
[
'id' => 121,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 139,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 1456,
'owner' => 'xv',
'name' => 'bjs',
],
[
'id' => 1896,
'owner' => 'xb',
'name' => 'bjs',
],
[
'id' => 1963,
'owner' => 'xb',
'name' => 'bjs',
]
];
// custom function to compare which ID is greater
function customCompare($a, $b)
{
if ($a['id'] === $b['id']) {
return 0;
}
return ($a['id'] < $b['id']) ? -1 : 1;
}
// sort the whole array
usort($array, "customCompare");
// print the whole array as pairs,
// if there is an unpair number
// the last one will be a single member
print_r(array_chunk($array, 2));
Split by characters of a paragraph into 3 equal parts in PHP
Check this one also
$string="this is my long text this is my long text this
is my long text this is my long text
this is my long text this is my
long text this is my long text this is my long text
this is my long text this is my long text";
$strlen=strlen($string);
$first= intval($strlen * (35/100));
$second=intval($strlen * (35/100));
$third=intval($strlen * 30/100);
$first_part=substr($string,0,$first);
$second_part=substr($string,$first,$second);
$third_part=substr($string,($first+$second));
Split string into 2 pieces by length using PHP
$first400 = substr($str, 0, 400);
$theRest = substr($str, 400);
You can rename your variables to whatever suits you. Those names are just for explanation. Also if you try this on a string less than 400 characters $theRest will be FALSE
Split text into equal-sized variables
Another solution is to:
$string = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.";
$string_length = strlen($string);
$chunks = 4; // change to desired.
$parts = ceil($string_length / $chunks); // Break string into the 4 parts.
$str_chunks = chunk_split($string, $parts);
$string_array = array_filter(explode(PHP_EOL, $str_chunks));
print_r($string_array);
Output:
Array
(
[0] => It is a long established fact t
[1] => hat a reader will be distracted
[2] => by the readable content of a p
[3] => age when looking at its layout.
)
PHP str word count divide equal parts
Without cutted words:
/**
* Splits a string with words in equal parts (PHP7)
*
* @since 1.0 DP0
* @version 1.0 DP0
*
* @param string words String with words
* @param int parts How many parts needed
*
* @return array Parts as rec. Array
*/
function split_words_into_parts( string $words, int $parts ) {
$words_array = explode( ' ', $words );
$words_cnt = count( $words_array );
$per_part = max( round( $words_cnt / $parts ), 1 );
$result = [];
/*--- Splitting full array ---*/
for( $i = 0; $i < $parts; $i++ )
$result[] = array_slice( $words_array, $i * $per_part, $per_part );
return $result;
}
/*--- Usage ---*/
$v = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
$result = split_words_into_parts( $v, 4 );
print_r( $result );
Split a given string into equal parts where number of sub strings will be of equal size and dynamic in nature?
You could give the length of the substrings and iterate until the end of the adjusted string.
function split(string, size) { var splitted = [], i = 0; string = string.match(/\S+/g).join(''); while (i < string.length) splitted.push(string.slice(i, i += size)); return splitted;}
console.log(...split('Hello World', 2));console.log(...split('Hello Worlds', 2));
Spliting one hour into four equal part
Use below code:-
function getInterval($time='01:00'){
$res = [];
$startTime = new DateTime($time);
for($i=0; $i<4;$i++){
$res[] = $startTime->format("H:i A").'<br>';
$startTime->add(new DateInterval('PT15M'));
}
return $res;
}
$result = getInterval('01:00');
echo '<pre>'; print_r($result);
As you have mentioned in your comment, you have an array like this ['10:00 am','11:00 am','12:00 pm','1:00 pm']
, then use below code.
$res=[];
$arr = ['10:00 am','11:00 am','12:00 pm','1:00 pm'];
foreach($arr as $record){
$ampm = explode(' ',$record)[1];
$time = explode(':',$record)[0];
for($i=0; $i<4;$i++){
$res[] = "$time:".($i*15 == 0?"00":$i*15)." $ampm";
}
}
echo '<pre>'; print_r($res);
Hope it will help you :-)
Related Topics
Yii2 Links Between Frontend and Backend (Advanced Template)
PHP - Hide Url (Get) Parameters
Http Header for Downloading Microsoft Word and Excel Files
Why Don't PHP and JavaScript's Timestamps Match
Why Are PHP's MySQL_ Functions Deprecated
Moving from MySQL to MySQLi or Pdo
How to Use PHP Inside CSS File
Symfony: Change Database Dynamically
Remove Double-Quotes from a JSON_Encoded String on the Keys
Switch of PHP Versions Not Working on MAC
How to Add Rel="Nofollow" to Links with Preg_Replace()
Using Ajax/Jquery to Refresh an Image