Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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