0

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:

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);
}
2
  • 1
    I'm not sure I understand the problem? Commented May 5, 2011 at 14:30
  • One str_word_count expects a string not an array, but my input is already temporarily exploded into an array... I'm basically trying to return index word count values of an array. So that Hello World "This is a test" Hello World with echo $matches[4] would become Hello World "test". Commented May 5, 2011 at 14:42

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry yeah the explode starts with the quote... and adds it back after the modifications. Hello World "This is a test"
Also my code is incorrect, im trying to count each word within quotes, get the index of each word and basically omit words by leaving certain values out. In simplest terms, always remove the fourth word in quotes...
I keep getting unexpected = on line 4 with that. I've been messing with it endlessly since i didn't get very many answers. Either way i appreciate your help. The most simple way to put what im looking for is take only words in quotes, label each one a number then take a few of the numbers and put them back in quotes. "[3] + [6]" would be "This Test"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.