lufa20 Posted April 25, 2013 Share Posted April 25, 2013 I am looking for a way to parse a file that contains a lot of text that is comma separated, yet it is not a csv. What i want is to find a keyword then echo that keyword and a few characters after that keyword, then look for another keyword and echo that keyword and some characters after that keyword...etc. Can someone point me in the right direction please? Quote Link to comment https://forums.phpfreaks.com/topic/277319-help-parse-text-file-find-keyword/ Share on other sites More sharing options...
Dathremar Posted April 25, 2013 Share Posted April 25, 2013 Take a look at ths php function: http://www.php.net/manual/en/function.preg-match.php Use a regex to find the matches and do what you want with that. Quote Link to comment https://forums.phpfreaks.com/topic/277319-help-parse-text-file-find-keyword/#findComment-1426658 Share on other sites More sharing options...
lufa20 Posted April 25, 2013 Author Share Posted April 25, 2013 Ok here is what I have and what I need it to look like: Sample file: {"RubricCriterion":[{"num":1,"value":"25","previous_version":null,"position":1,"name":"Writing","description":null,"rubric":352600,"criterion_scales":[10985652,10985648,10985644,10985640],"id":3423222},{"num":2,"value":"25","previous_version":null,"position":2,"name":"Grammar","description":null,"rubric":352600,"criterion_scales":[10985651,10985647,10985643,10985639],"id":3423223},{"num":3,"value":"25","previous_version":null,"position":3,"name":"Punctuation","description":null,"rubric":352600,"criterion_scales":[10985650,10985646,10985642,10985638],"id":3423224},{"num":4,"value":"25","previous_version":null,"position":4,"name":"Spelling","description":null,"rubric":352600,"criterion_scales":[10985649,10985645,10985641,10985637],"id":3423225}],"Rubric":[{"cv_loaded":"1","criterion":[3423222,3423223,3423224,3423225],"assignments":[],"id":352600,"papers_scored":"0","owner":1006900964,"criterion_scales_all":[10985652,10985648,10985644,10985640,10985651,10985647,10985643,10985639,10985650,10985646,10985642,10985638,10985649,10985645,10985641,10985637],"name":"MyRubric","deleted":0,"description":null,"distribute_criterion_percentage":0,"total_points":null,"scoring_method":2,"scale_values":[3343228,3343229,3343230,3343231],"rubric_group":null}],"RubricScale":[{"num":1,"value":"25","position":1,"name":"Not Acceptable","id":3343228,"rubric":352600},{"num":2,"value":"50","position":2,"name":"Beginner","id":3343229,"rubric":352600},{"num":3,"value":"75","position":3,"name":"Competent","id":3343230,"rubric":352600},{"num":4,"value":"100","position":4,"name":"Proficient","id":3343231,"rubric":352600}],"RubricCriterionScale":[{"scale_value":3343228,"value":"0","criterion":3423222,"id":10985652,"description":"poor writing"},{"scale_value":3343229,"value":"0","criterion":3423222,"id":10985648,"description":"begineer writing"},{"scale_value":3343230,"value":"0","criterion":3423222,"id":10985644,"description":"competent writing"},{"scale_value":3343231,"value":"0","criterion":3423222,"id":10985640,"description":"proficient writing"},{"scale_value":3343228,"value":"0","criterion":3423223,"id":10985651,"description":"poor grammar"},{"scale_value":3343229,"value":"0","criterion":3423223,"id":10985647,"description":"beginner grammar"},{"scale_value":3343230,"value":"0","criterion":3423223,"id":10985643,"description":"competent grammar"},{"scale_value":3343231,"value":"0","criterion":3423223,"id":10985639,"description":"proficient grammar"},{"scale_value":3343228,"value":"0","criterion":3423224,"id":10985650,"description":null},{"scale_value":3343229,"value":"0","criterion":3423224,"id":10985646,"description":null},{"scale_value":3343230,"value":"0","criterion":3423224,"id":10985642,"description":null},{"scale_value":3343231,"value":"0","criterion":3423224,"id":10985638,"description":null},{"scale_value":3343228,"value":"0","criterion":3423225,"id":10985649,"description":null},{"scale_value":3343229,"value":"0","criterion":3423225,"id":10985645,"description":null},{"scale_value":3343230,"value":"0","criterion":3423225,"id":10985641,"description":null},{"scale_value":3343231,"value":"0","criterion":3423225,"id":10985637,"description":null}]} Desired Output (i will format the html to make it pretty) Quote Link to comment https://forums.phpfreaks.com/topic/277319-help-parse-text-file-find-keyword/#findComment-1426661 Share on other sites More sharing options...
Dathremar Posted April 25, 2013 Share Posted April 25, 2013 Use this http://www.php.net/manual/en/function.json-decode.php to get the result out of that string. That is JSON object that you can use. Quote Link to comment https://forums.phpfreaks.com/topic/277319-help-parse-text-file-find-keyword/#findComment-1426662 Share on other sites More sharing options...
lufa20 Posted April 25, 2013 Author Share Posted April 25, 2013 (edited) why does it work when I insert it into $string and it doesn't when I don't <? $file = "rbc/$_GET[parse]"; $string = json_decode(file_get_contents($file, true)); foreach ($string->RubricCriterion as $rubric) { echo $rubric->name; } ?> EDIT [ignace]: I accidently edited your post mistaken it for quoting you so the wording may be different, my apologies Edited April 26, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/277319-help-parse-text-file-find-keyword/#findComment-1426675 Share on other sites More sharing options...
ignace Posted April 26, 2013 Share Posted April 26, 2013 you mean like this? <?php $file = "rbc/" . basepath($_GET['parse']); if (!file_exists($file)) { printf("file %s does not exist", $file); exit; } foreach (json_decode(file_get_contents($file, true))->RubricCriterion as $rubric) { echo $rubric->name; } Quote Link to comment https://forums.phpfreaks.com/topic/277319-help-parse-text-file-find-keyword/#findComment-1426763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.