Jump to content

I used $locarray = preg_split($wordstring, -1, PREG_SPLIT_OFFSET_CAPTURE); cant


josheeg

Recommended Posts

I used $locarray = preg_split($wordstring, -1, PREG_SPLIT_OFFSET_CAPTURE); to devide a string up... now the offset of the words that it split up is important I can see it by using print_r like this...

print_r($locarray);

 

Then I get back this...

Array ( [0] => Array ( [0] => [1] => 0 ) [1] => Array ( [0] => The [1] => 1 ) [2] => Array ( [0] => adventure's [1] => 5 ) [3] => Array ( [0] => not [1] => 17 ) [4] => Array ( [0] => over [1] => 21 ) [5] => Array ( [0] => yet! [1] => 26 ) [6] => Array ( [0] => All-new [1] => 31 ) [7] => Array ( [0] => episodes [1] => 39 ) [8] => Array ( [0] => of [1] => 48 ) [9] => Array ( [0] => Stargate [1] => 51 ) [10] => Array ( [0] => SG-1 [1] => 60 ) [11] => Array ( [0] => are [1] => 65 ) [12] => Array ( [0] => coming [1] => 69 ) [13] => Array ( [0] => in==2007 [1] => 76 ) [14] => Array ( [0] => as [1] => 86 ) [15] => Array ( [0] => the [1] => 89 ) [16] => Array ( [0] => team [1] => 93 ) [17] => Array ( [0] => faces [1] => 98 ) [18] => Array ( [0] => its [1] => 104 ) [19] => Array ( [0] => greatest [1] => 108 ) [20] => Array ( [0] => challenge: [1] => 117 ) [21] => Array ( [0] => stopping==the [1] => 128 ) [22] => Array ( [0] => Ori [1] => 142 ) [23] => Array ( [0] => invasion [1] => 146 ) [24] => Array ( [0] => of [1] => 155 ) [25] => Array ( [0] => the [1] => 158 ) [26] => Array ( [0] => Milky [1] => 162 ) [27] => Array ( [0] => Way [1] => 168 ) [28] => Array ( [0] => galaxy!== [1] => 172 ) )

 

How do I get the offsets to show up individually I can't seem to acsess them and $test=$locarray[0][0]; does not print out something but 01 does print out a 0...

please help

 

 

The formatting there is quite weird.  But the array looks normal.  You should be able to access it like this:

 

foreach ($locarray as $l) {
  print "-{$l[0]}- at offset {$l[1]}\n";
}

 

$locarray[0][0] prints nothing out because the element there is an empty string.  [0][0] is the first string, [0][1] the first offset, [1][0] the second string, [1][1] the second offset, etc etc

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.