Created
August 24, 2019 20:02
-
-
Save cereal-s/940304a7d23ddef65fe57b4b70baa399 to your computer and use it in GitHub Desktop.
Tokenize string - strtok()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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