tokenize - PHP Tokens From a String -
let's have string looks this: token1 token2 tok3
and want of tokens (specifically strings between spaces), , position (offset) , length).
so want result looks this:
array( array( 'value'=>'token1' 'offset'=>0 'length'=>6 ), array( 'value'=>'token2' 'offset'=>7 'length'=>6 ), array( 'value'=>'tok3' 'offset'=>14 'length'=>4 ), )
i know can done looping through characters of string , can simpy write function this.
i wondering, php have built-in efficiently or @ least part of this?
i looking suggestions , appreciate given. thanks
you can use preg_match_all
preg_offset_capture flag:
$str = 'token1 token2 tok3'; preg_match_all('/\s+/', $str, $matches, preg_offset_capture); var_dump($matches);
then need replace items in $matches[0]
this:
function update($match) { return array( 'value' => $value[0], 'offset' => $value[1], 'length' => strlen($value[0])); } array_map('update', $matches[0]); var_dump($matches[0]);
Comments
Post a Comment