lockdownd7 Posted August 30, 2009 Share Posted August 30, 2009 I'll try to keep it simple. I'm trying to parse something of this syntax into an array: {one|two|three|four} One, two, three, etc. are just examples; the idea is that I could put any words in those brackets, and send them to an array. BTW, the above string is stored in "template1.txt". Here's what I've got: $template = "template1.txt"; $fh = fopen($template, 'r') or die("can't open file"); $templatetext = fread($fh, filesize($template)); while($offset = strpos($templatetext, "|", $offset + 1)){ $locations[] = $offset; } for ($i = 0; $i < count($locations); $i++) { $locationsaddone = $locations[i] + 1; $countlessone = count($locations) - 1; $beginning = $locationsaddone; if ($i === $countlessone) { $end = strpos($templatetext, "}"); } /*IF I HAD TO GUESS, I'D SAY THE ERROR IS HERE */ else { $end = $locations[$i+1]; } fseek($fh, $locationsaddone); $total = $end - $beginning; $word[$i] = fread($fh, $total); /* OR HERE */ } I'm open to any and all suggestions... even if you can't figure out what's wrong, if you can suggest an alternative solution.... I'd really appreciate it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/172501-solved-trying-to-read-strings-from-a-file-to-put-in-array-help-please/ Share on other sites More sharing options...
.josh Posted August 31, 2009 Share Posted August 31, 2009 If your file looks something like this... {one|two|three|four} {one|two|three|four} {one|two|three|four} {one|two|three|four} etc.. then you can simplify the process by not storing it with the { ... } as they are unnecessary. $template = "template1.txt"; $rows = file($template); foreach($rows as $row) { $row = trim($row); $row = ltrim($row,'{'); $row = rtrim($row,'}'); $word[] = explode('|',$row); } This will for instance produce: Array ( [0] => Array ( [0] => 1one [1] => two [2] => three [3] => four ) [1] => Array ( [0] => 2one [1] => two [2] => three [3] => four ) [2] => Array ( [0] => 3one [1] => two [2] => three [3] => four ) ) Quote Link to comment https://forums.phpfreaks.com/topic/172501-solved-trying-to-read-strings-from-a-file-to-put-in-array-help-please/#findComment-909437 Share on other sites More sharing options...
lockdownd7 Posted August 31, 2009 Author Share Posted August 31, 2009 Well after looking at your code, I made some real changes: $template = "template1.txt"; $fh = fopen($template, 'r') or die("can't open file"); $templatetext = fread($fh, filesize($template)); $words = explode("|", $templatetext); $lastitem = end($words); foreach ($words as &$value) { if (strstr($value, "{") == "$value"){ $value = ltrim($value, "{"); } elseif($lastitem == $value){ $value = rtrim($value, "}"); } } But for some reason, while it does trim the "{" it won't trim the "}" . And I honestly can't figure it out. The elseif executes at the right time, but it's like it's ignoring the statement. Quote Link to comment https://forums.phpfreaks.com/topic/172501-solved-trying-to-read-strings-from-a-file-to-put-in-array-help-please/#findComment-909486 Share on other sites More sharing options...
.josh Posted August 31, 2009 Share Posted August 31, 2009 rtrim, ltrim and trim, trim off the things on the end of the string. The end of your string is not the }. It's a newline char: \n. That's why I had the trim in there first: to first remove the newline char. Quote Link to comment https://forums.phpfreaks.com/topic/172501-solved-trying-to-read-strings-from-a-file-to-put-in-array-help-please/#findComment-909491 Share on other sites More sharing options...
lockdownd7 Posted August 31, 2009 Author Share Posted August 31, 2009 Newline character.... I would have never thought of that! Tyvm, I really do appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/172501-solved-trying-to-read-strings-from-a-file-to-put-in-array-help-please/#findComment-909497 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.