Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active February 26, 2024 14:28
Show Gist options
  • Save vijinho/3d66fab3270fc377b8485387ce7e7455 to your computer and use it in GitHub Desktop.
Save vijinho/3d66fab3270fc377b8485387ce7e7455 to your computer and use it in GitHub Desktop.
Escape markdown in php
<?php
/**
* Escape markdown text
*
* @param string $text the markdown text to escape
*
* @return string escaped text
*/
function markdown_escape($text) {
return str_replace([
'\\', '-', '#', '*', '+', '`', '.', '[', ']', '(', ')', '!', '&', '<', '>', '_', '{', '}', '|'], [
'\\\\', '\-', '\#', '\*', '\+', '\`', '\.', '\[', '\]', '\(', '\)', '\!', '\&', '\<', '\>', '\_', '\{', '\}', '\|',
], $text);
}
@MartinGoesToSauerland
Copy link

It would be good to add the pipe ( | ) to your list. Otherwise great and thank you!

@vijinho
Copy link
Author

vijinho commented Feb 7, 2024

Lol, I can't believe people still use my stuff, not coded for over a year. OK added a pipe!

@looqa
Copy link

looqa commented Feb 26, 2024

Lol, I can't believe people still use my stuff, not coded for over a year. OK added a pipe!

Thank you, also used it

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