Created
June 4, 2015 13:45
-
-
Save hans2103/670e0e7999f66fac3cef to your computer and use it in GitHub Desktop.
Replace space by underscore in pattern
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 | |
/** | |
* replace space by underscore in given pattern | |
* written by Github user @pjdevries | |
* | |
* run from command line | |
* $ php unspace-anchor-names.php name-input-file > name-output-file | |
* | |
*/ | |
function pregReplaceCallback($matches) { | |
if (count($matches) > 1) { | |
return str_replace($matches[1], str_replace(' ', '_', trim($matches[1])), $matches[0]); | |
} | |
return $matches[0]; | |
} | |
if (count($argv) > 1) { | |
$file = $argv[1]; | |
} else { | |
$file = 'php://stdin'; | |
} | |
$fp = fopen($file, 'r'); | |
while (($line = fgets($fp)) !== false) { | |
$pattern = '/<a\s*name=\\\"([^"]*)\\\"[^>]*>/'; | |
echo preg_replace_callback($pattern, 'pregReplaceCallback', $line); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment