Skip to content

Instantly share code, notes, and snippets.

@hans2103
Created June 4, 2015 13:45
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 hans2103/670e0e7999f66fac3cef to your computer and use it in GitHub Desktop.
Save hans2103/670e0e7999f66fac3cef to your computer and use it in GitHub Desktop.
Replace space by underscore in pattern
<?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