Skip to content

Instantly share code, notes, and snippets.

@JulianLaval
Created September 5, 2013 10:59
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 JulianLaval/6448711 to your computer and use it in GitHub Desktop.
Save JulianLaval/6448711 to your computer and use it in GitHub Desktop.
Simple function to parse inline Markdown links to HTML.
<?php
// PARSE INLINE MARKDOWN LINKS
function markdown_links($x) {
preg_match_all('/\[(.*?)\]\((.*?)\)/', $x, $matches);
if(!empty($matches)){
foreach($matches[0] as $key => $match){
$x_replace = '<a href="'.$matches[2][$key].'" target="_blank">'.$matches[1][$key].'</a>';
$x = str_replace($matches[0][$key], $x_replace, $x);
}
}
return $x;
}
?>
@JulianLaval
Copy link
Author

Example usage:

 $text = "Follow me on [GitHub](https://github.com/JulianLaval)";

 $converted_text = markdown_links($text);
 // RESULT: $converted_text = 'Follow me on <a href="https://github.com/JulianLaval">GitHub</a>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment