Print list of defined str_word_count matches within exploded array by index
I previously had some help with this matter from @HSZ but have had trouble getting the solution to work with an existing array. What im trying to do is explode quotes, make all words uppercase plus get each words index value and only echo the ones i define then implode. In simplest terms, always remove the 4th word within quotations or the 3rd and 4th... This could probably done with regex as well.
Example:
Hello [1] => World [2] => This [3] => Is [4] => a [5] => Test ) 6
only outputs the numbers i define, such as 1 - (Hello) and [2] - (World) leaving out [3], [4], [5] and [6] or This is a test leaving only Hello World, or 1 and [6] for Hello Test...
Such as:
开发者_JAVA技巧echo $data[1] + ' ' + $data[6]; //Would output index 1 and 6 Hello and Test
Existing Code
if (stripos($data, 'test') !== false) {
$arr = explode('"', $data);
for ($i = 1; $i < count($arr); $i += 2) {
$arr[$i] = strtoupper($arr[$i]);
$arr[$i] = str_word_count($arr, 1); // Doesnt work with array of course.
}
$arr = $matches[1] + ' ' + $matches[6];
$data = implode('"', $arr);
}
Assuming $data
is 'Hello world "this is a test"'
, $arr = explode('"', $data)
will be the same as:
$arr = array (
[0] => 'Hello World',
[1] => 'This is a test'
)
If you want to do things with this is a test
, you can explode it out using something like $testarr = explode(' ', $arr[1]);
.
You can then do something like:
$matches = array();
foreach ($testarr as $key => $value) {
$value = strtoupper($value);
if(($key+1)%4 == 0) { // % is modulus; the result is the remainder the division of two numbers. If it's 0, the key+1 (compensate for 0 based keys) is divisible by 4.
$matches[] = $value;
}
}
$matches = implode('"',$matches);
精彩评论