Jump to content

[SOLVED] Trying to read strings from a file to put in array.. help please


Recommended Posts

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.

 

 

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
        )

)

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.  :wtf:

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.