0) { // Get the entire match array. Should be the only one there anyway. $matches = $matches[0]; foreach ($matches as $match) { // Get rid of any trailing . in case the cross reference is the // end of a sentence and the match occurred. $match = rtrim($match, "."); // For each cross reference match we found, replace with // markdown link in our map and create a text link to the title $text = $ref_link_text === null ? $match : $ref_link_text; $replace = "[" . $text . "]"; if (!$update_only){ $replace .= "(#" . $section_map[$match] . ")"; } $line = str_replace($match, $replace, $line); } } $update .= $line; } file_put_contents($md_file, $update); } function map_word_sections_to_markdown_sections($section_map_file, $md_file) { $md_contents = file_get_contents($md_file); $toc_start = strpos($md_contents, "**Table of Contents**"); $toc_end = strpos($md_contents, "#Introduction"); $md_toc = substr($md_contents, $toc_start, $toc_end); $md_section_pattern = "/\(#([a-z0-9\-_]+)\)/"; $matches = array(); preg_match_all($md_section_pattern, $md_toc, $matches); // Just want the sub-match only $matches = $matches[1]; $sm_contents = file($section_map_file, FILE_IGNORE_NEW_LINES); $num_elements = count($sm_contents); for ($i = 0; $i < $num_elements; $i++) { if (!strpos($sm_contents[$i], $matches[$i])) { $sm_contents[$i] .= "," . $matches[$i]; } } file_put_contents($section_map_file, implode(PHP_EOL, $sm_contents)); } function main($argv) { $opts = getopt("hm:s:t:u"); if (array_key_exists("h", $opts)) { die("php xreference.php -m -s