TheRealDeal
TheRealDeal

Reputation: 25

Is it possible to combine strtok() and substr()?

I've recently started working with PHP and am trying to make a list from a .txt BUT removing all the unnecessary components. a line got this

item = 0    egg    came_from_chicken

I want to remove the item = and the came_from_chicken which leaves me with 0 egg. Now after some searching I've found substr() to remove the first 5 characters of each of my lines. Later I've also found that strtok() can remove the rest of the unwanted text after the second tab. Unfortunately I cannot combine these. So my question is: How to remove the first 5 chars from each line and remove everything after the second tab from each line?

I've got this so far:

<?php
$lines = file('../doc/itemlist.txt');
$newf = array();
foreach ($lines as $line) {
    $newf[] = strtok($line, "\t") . "\t" . strtok("\t");
}
var_dump($newf);
?>

This works like a charm to remove everything after egg but still have to remove item =.

Upvotes: 1

Views: 138

Answers (5)

Unsigned
Unsigned

Reputation: 9916

This approach will work with any string preceeding the = symbol.

foreach ($lines as $line) {
  $newf[] = implode("\t", array_slice(explode("\t", trim(explode('=', $line, 2)[1])), 0, 2));
}

Upvotes: 0

CAllen
CAllen

Reputation: 836

I know this is not the answer you are looking for using strtok but I believe it would be much easier to do the following code below:

<?php
    $lines  = '../doc/itemlist.txt';

    // array to store all data
    $newf   = array();

    foreach ($lines as $line) {
        // you can do an explode which will turn it into an array and you can then get any values you want
        // $newf [] = strtok ($line, "\t") . "\t" . strtok("\t"); // throw away

        // lets say we use [item = 0    egg    came_from_chicken] as our string
        // we split it into an array for every tab or spaces found
        $values     = preg_split ('/\s+/', $line);
        //returns
        //  Array
        //  (
        //      [0] => item
        //      [1] => =
        //      [2] => 0
        //      [3] => egg
        //      [4] => came_from_chicken
        //      [5] => 
        //  )

        // lastly store your values which would be sub 2 and sub 4
        $newf []    = $values [2] . ' ' . $values [3];
    }

    var_dump($newf);

    // return
    // array (size=3)
    // 0 => string '0 aaa' (length=5)
    // 1 => string '1 bbb' (length=5)
    // 2 => string '2 ccc' (length=5)
?>

Upvotes: 0

CodingMageSheen
CodingMageSheen

Reputation: 154

Assuming that you will be receiving similar patterns to the example you gave.

You can just do a simple line of:

$str = "item = 0    egg    came_from_chicken"; 
$parts = preg_split('/\s+/', $str);
echo $parts[2] . $parts[3];

Upvotes: 0

user4035
user4035

Reputation: 23729

You can use a regular expression:

<?php
$lines = file('./file.txt');
$newf = array();
foreach ($lines as $line) {
    $newf[] = preg_replace('/.*=\s*(.*)\t.*/s', '$1', $line);
}

var_dump($newf);

Output:

array(1) {
  [0]=>
  string(5) "0  egg"
}

Upvotes: 0

rickdenhaan
rickdenhaan

Reputation: 11298

The quick-and-dirty way is to just wrap it all:

$newf[] = substr(strtok($line, "\t") . "\t" . strtok("\t"), 5);

But, I personally have a dislike for strtok() (can't explain why, I just don't like it). If you don't need to strip everything off from the second tab, but from the last tab (in your example the second tab is the last tab), I would use strrpos() to find the location of the last tab, and dump that into substr():

$newf[] = substr($line, 5, strrpos($line, "\t")-5);

That -5 is to compensate for the 5 characters you strip off from the beginning. If you need to start at character 6 instead of 5, you should also subtract 6 from whatever strrpos() returns.

Edit Never mind that whole last part, I just saw the example format you posted and you really need the second tab instead of the last tab.

Upvotes: 0

Related Questions