Skip to content

Instantly share code, notes, and snippets.

@cereal-s
Created August 24, 2019 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cereal-s/940304a7d23ddef65fe57b4b70baa399 to your computer and use it in GitHub Desktop.
Save cereal-s/940304a7d23ddef65fe57b4b70baa399 to your computer and use it in GitHub Desktop.
Tokenize string - strtok()
<?php
/**
* Break lines for each group of values
* in the SQL insert statements.
*
* It can be used also for simple strings.
*
* Usage script.php < IN > OUT
*
* @see https://php.net/strtok
*/
ini_set('auto_detect_line_endings', TRUE);
// open input and output streams
$in = fopen('php://stdin', 'r');
$out = fopen('php://stdout', 'w');
// set the character to find
// @NOTE if set multiple characters
// strtok() will find ANY of them
$find = ')';
// Execute
while ( ! feof($in)) {
while (($line = fgets($in)) !== false) {
$line = trim($line);
// process the line read.
$tok = strtok($line, $find);
while(false !== $tok) {
$previous_tok = $tok;
$tok = strtok($find);
// verify if next loop will exit or not
// and write the new line
if(false !== $tok) {
fwrite($out, trim($previous_tok) . $find . PHP_EOL);
} else {
fwrite($out, trim($previous_tok) . PHP_EOL);
}
}
}
}
// unset strtok()
$tok = strtok('', '');
fclose($out);
fclose($in);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment