Lets say I have a string:
$string = "This is my test case for an example."
If I do explode based on ' ' I get an
Array('This','is','my','test','case','for','an','example.');
What I want is an explode for every other space:
Array('This is','my test','case for','an example.').
The string may have an odd # of words, so the last item in the array may not contain two words.
Anyone know how to do this?
I would look through the results, and concatenate the strings after the fact.
$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);
yields:
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 to match a single word at the end of the sentence
A function which can be used for different delimiters and numbers.
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;
}
Test code
var_dump(explodeEveryNth(' ', 'This is a test string', 2));
$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);
$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);
Something You Can Re-Use for Other Scenarios: (always better IMO).
While probably not the most elegant solution, this does follow the general concept syntax of other PHP core functions...
In any case... This uses recursion. It is flexible in that it allows you to specify the size of the chunk (in case you'd like to do that down the road or for a different project). I did this as more of a personal challenge to see what I could come up with.
<?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);
If this chunk_explode function sucks, let me know so I can learn from my mistakes.
There are 75 array functions in PHP, let's try to use them instead of for loops!!
I like Kyle's function name. (I'll assume you aren't running 5.3 and suffer with 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);
}
$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."
}
Well obviously this isn't the best solution, but it was fun figuring it out in my own way. Still so 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!
来源:https://stackoverflow.com/questions/840807/explode-over-every-other-word