Skip to content

Commit

Permalink
Add function to escape discord markdown (#586)
Browse files Browse the repository at this point in the history
* Add function to escape discord markdown

Used this function in my bot, useful to escape markdown and formatting symbols with backslashes

* Update src/Discord/functions.php

added parameter type

Co-authored-by: David <[email protected]>

* Update functions.php escapeMarkdown

Added explanation and also added # character to escape channel mentions

Co-authored-by: David <[email protected]>
  • Loading branch information
SQKo and davidcole1340 authored Aug 24, 2021
1 parent 8e9cbf9 commit c74c3e3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Discord/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,17 @@ function normalizePartId($id_field = 'id')
return $part;
};
}

/**
* Escape various Discord formatting and markdown into a plain text:
* _Italics_, **Bold**, __Underline__, ~~Strikethrough~~, ||spoiler||
* `Code`, ```Code block```, > Quotes, >>> Block quotes
* #Channel @User
* A backslash will be added before the each formatting symbol
*
* @return string the escaped string unformatted as plain text
*/
function escapeMarkdown(string $text): string
{
return addcslashes($text, '#*:>@_`|~');
}

0 comments on commit c74c3e3

Please sign in to comment.