php - preg_match - Regular Expression To Create Array -


my data -

{'/users/aaron/applications/developer-vagrant/web/g.php': {'total': 22}} {'/users/aaron/.vim/autoload/timetap.vim': {'total': 0}} {'/users/aaron/.vimrc': {'total': 5}} {'/users/aaron/documents/programming/php/timetapcli/composer.json': {'total': 144}} {'/users/aaron/documents/programming/php/timetapcli/timetap.php': {'total': 351}} {'/users/aaron/box/linux/.vim/autoload/timetap.vim': {'total': 37}} {'/users/aaron/box/cats.tex': {'total': 184}} 

i attempting create regular expression can convert above array using preg_match. want data -

i want array of of data believe should below-

 array (    [0] => array (       [0] => '/users/aaron/box/cats.tex'       [1] => array (                   [total] =>'184'              )    }  } 

my attempt @ preg_match -

$subject = file_get_contents('/users/aaron/.timetap/full.db'); $pattern = '{...}'; preg_match($pattern, substr($subject,3), $matches, preg_offset_capture); 

what pattern in order take above data , turn array in php? there php function convert array without using preg_match?

you're regex doesn't make sense have it. 1 thing missing delimiters. {, }, , . special regex characters should escaped. looks json data structure json functions might of use you. if still want go regex here's how i'd presuming data structure consistent.

<?php $string = "{'/users/aaron/applications/developer-vagrant/web/g.php': {'total': 22}} {'/users/aaron/.vim/autoload/timetap.vim': {'total': 0}} {'/users/aaron/.vimrc': {'total': 5}} {'/users/aaron/documents/programming/php/timetapcli/composer.json': {'total': 144}} {'/users/aaron/documents/programming/php/timetapcli/timetap.php': {'total': 351}} {'/users/aaron/box/linux/.vim/autoload/timetap.vim': {'total': 37}} {'/users/aaron/box/cats.tex': {'total': 184}}"; $pattern = '~^\{(.*)\}$~m'; $data[] = preg_replace_callback($pattern, function($matches) {     global $output_data;     preg_match("~'(.*?)'\s*:\s*\{'(.*?)'\s*:\s*(\d+)\}~", $matches[1], $output);     $output_data[$output[1]] = array($output[2] => $output[3]); }, $string); print_r($output_data); 

output:

array (     [/users/aaron/applications/developer-vagrant/web/g.php] => array         (             [total] => 22         )      [/users/aaron/.vim/autoload/timetap.vim] => array         (             [total] => 0         )      [/users/aaron/.vimrc] => array         (             [total] => 5         )      [/users/aaron/documents/programming/php/timetapcli/composer.json] => array         (             [total] => 144         )      [/users/aaron/documents/programming/php/timetapcli/timetap.php] => array         (             [total] => 351         )      [/users/aaron/box/linux/.vim/autoload/timetap.vim] => array         (             [total] => 37         )      [/users/aaron/box/cats.tex] => array         (             [total] => 184         )  ) 

here links information on functions/modifiers i've used.

  1. http://php.net/manual/en/reference.pcre.pattern.modifiers.php
  2. http://php.net/manual/en/function.preg-replace-callback.php
  3. http://php.net/manual/en/function.preg-match.php

i'll write of parts used here in bit. if have particular questions please post.

explanation of happening...

the ~ delimiters tell regex engine expression starts @ ends. m on outside modifier tells treat each line as string. ^ , $ tell match start , end of "string", in case each line because of m modifier. \ before { escape curly brace has special context in regex. . character , * quantifier meaning 0 or more occurrences. when these paired means 0 or more of characters. () around capturing group stores inside it, , \} stop last curly brace. {'/users/aaron/applications/developer-vagrant/web/g.php': {'total': 22}} end '/users/aaron/applications/developer-vagrant/web/g.php': {'total': 22}. pass function because want filter down further. use global here because inside of anonymous function , want accessible when done.the '(.*?)' searching between single quotes. known lazy/non greedy, ? makes stop @ first occurrence of next character (the single quote). \s* amount of whitespace. rest of regex here should decipherable previous descriptions. $matches[1] because want first grouped value preg_replace_callback $matches[0] found (same preg_match). on final line assign our global variable new value.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -