Blow up all the other words

Let's say I have a line:

$string = "This is my test case for an example."

If I explode based on "I get

Array('This','is','my','test','case','for','an','example.');

I want to explode for any other space:

Array('This is','my test','case for','an example.').

A string may contain an odd number of words, so the last element in the array may not contain two words.

Does anyone know how to do this?

+3
source share
9 answers

I looked at the results and merged the lines after the fact.

+11
source
$matches = array();
preg_match_all('/([A-Za-z0-9\.]+(?: [A-Za-z0-9\.]+)?)/',
       'This is my test case for an example.',$matches);

print_r($matches);

gives:

Array
(
  [0] => Array
    (
        [0] => This is
        [1] => my test
        [2] => case for
        [3] => an example.
    )

  [1] => Array
    (
        [0] => This is
        [1] => my test
        [2] => case for
        [3] => an example.
    )

)

update fixed it according to one word at the end of a sentence

+8
source

, .

function explodeEveryNth($delimiter, $string, $n) {
    $arr = explode($delimiter, $string);
    $arr2 = array_chunk($arr, $n);
    $out = array();
    for ($i = 0, $t = count($arr2); $i < $t; $i++) {
        $out[] = implode($delimiter, $arr2[$i]);
    }
    return $out;
}

var_dump(explodeEveryNth(' ', 'This is a test string', 2));
+3

$string = "This is my test case for an example.";

preg_match_all("/[a-zA-Z0-9]+\ [a-zA-Z0-9]+/", $string, $matches);
print_r($matches);

>

+2
$matches = array();
preg_match_all('/\S+(?:\s[A-Za-z0-9.]+|$)/',
    'This is my test case for an example.',
    $matches
);
print_r($matches);
preg_match_all('/\S+(?:\s[A-Za-z0-9.]+|$)/',
    'This is my test case for example.',
    $matches
);
print_r($matches);
+1

-, : ( IMO).

, , , PHP...

... . , ( ). , , .

<?php
function chunk_explode($glue=' ',$pieces='',$size=2,$final=array()) {
    if(!is_string($pieces) && !is_array($pieces)) 
        return false;

    if(is_string($pieces))
        $pieces = explode($glue,$pieces);

    $num_pieces = sizeof($pieces);
    if($num_pieces <= 0) 
       return $final;

    if($num_pieces >= $size) {
        $arr_chunk = array_chunk($pieces, $size);
        array_push($final,implode($glue,$chunk[0]));
        for($i=0;$i<=$size;$i++) { array_shift($pieces); }
        return chunk_explode($glue,$pieces,$size,$final);
    }
    array_push($final,implode($glue,$pieces));
    return $final;
}
$string = "This is my test case for an example.";
chunk_explode(' ',$string,3);

chunk_explode , , .

+1

PHP 75 , !

Kyle. (, 5.3 create_function.)

 function chunk_explode($string, $chunks = 2, $delim = ' ') {
     $A = explode($delim, $string);
     $A = array_chunk($A, $chunks);
     return array_map(
         create_function('$x',
            'return implode(\'' . $delim . '\',$x);'), $A);
 }
+1
    $str = "This is my test case for an example.";
    $arr = split(' ', $str);
    $newArr = array();
    $count = count($arr);
    for($i=0;$i<$count;$i = $i + 2) {
        $newArr[] = $arr[$i] . ' ' . $arr[$i+1];
    }


array(4) {
  [0]=>
  string(7) "This is"
  [1]=>
  string(7) "my test"
  [2]=>
  string(8) "case for"
  [3]=>
  string(11) "an example."
}
0

Well, obviously, this is not the best solution, but it was great to understand it in your own way. Much to learn ...

function solveThisPuzzle($string) {

$modified_string = preg_replace('(\s)', '+', $string, -1, $count);  
$words = explode('+', $modified_string);

$phrases_arr = array();

for($i = 1; $i < $count+1; $i++) {
    if(($i % 2)) {
        $phrase = $words[$i-1].' '.$words[$i];
        $phrases_arr[] = $phrase;
        unset($phrases_arr[$i]);
    } elseif($i == $count) {
            $phrase = $words[$i];
            $phrases_arr[] = $phrase;
        } else {            
            $phrase = NULL;
        }
}

foreach($phrases_arr as $final_phrase) {
    $solution .= $final_phrase.'<br />';
}

    return $solution;

}

$string = "This is my test case for an example, huzzah!";
echo solveThisPuzzle($string);

This is
my test
case for
an example,
huzzah!
0
source

Source: https://habr.com/ru/post/1707956/


All Articles