bbcode documentation: fix broken bookmark form: #^[url]...[/url]

- Prevents the advance recognition of `^` in tags before bbcode()
- Formatting
- Documentation
This commit is contained in:
Hypolite Petovan 2016-10-30 23:59:14 -04:00
parent a65479ccfd
commit c2e17cb9c4
1 changed files with 28 additions and 25 deletions

View File

@ -769,71 +769,74 @@ function activity_match($haystack,$needle) {
}} }}
if(! function_exists('get_tags')) {
/** /**
* Pull out all #hashtags and @person tags from $s; * Pull out all #hashtags and @person tags from $string;
* We also get @person@domain.com - which would make * We also get @person@domain.com - which would make
* the regex quite complicated as tags can also * the regex quite complicated as tags can also
* end a sentence. So we'll run through our results * end a sentence. So we'll run through our results
* and strip the period from any tags which end with one. * and strip the period from any tags which end with one.
* Returns array of tags found, or empty array. * Returns array of tags found, or empty array.
* *
* @param string $s * @param string $string Post content
* @return array * @return array List of tag and person names
*/ */
function get_tags($s) { function get_tags($string) {
$ret = array(); $ret = array();
// Convert hashtag links to hashtags // Convert hashtag links to hashtags
$s = preg_replace("/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism", "#$2", $s); $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
// ignore anything in a code block // ignore anything in a code block
$s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s); $string = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $string);
// Force line feeds at bbtags // Force line feeds at bbtags
$s = str_replace(array("[", "]"), array("\n[", "]\n"), $s); $string = str_replace(array('[', ']'), array("\n[", "]\n"), $string);
// ignore anything in a bbtag // ignore anything in a bbtag
$s = preg_replace('/\[(.*?)\]/sm','',$s); $string = preg_replace('/\[(.*?)\]/sm', '', $string);
// Match full names against @tags including the space between first and last // Match full names against @tags including the space between first and last
// We will look these up afterward to see if they are full names or not recognisable. // We will look these up afterward to see if they are full names or not recognisable.
if(preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/',$s,$match)) { if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
foreach($match[1] as $mtch) { foreach ($matches[1] as $match) {
if(strstr($mtch,"]")) { if (strstr($match, ']')) {
// we might be inside a bbcode color tag - leave it alone // we might be inside a bbcode color tag - leave it alone
continue; continue;
} }
if(substr($mtch,-1,1) === '.') if (substr($match, -1, 1) === '.') {
$ret[] = substr($mtch,0,-1); $ret[] = substr($match, 0, -1);
else } else {
$ret[] = $mtch; $ret[] = $match;
}
} }
} }
// Otherwise pull out single word tags. These can be @nickname, @first_last // Otherwise pull out single word tags. These can be @nickname, @first_last
// and #hash tags. // and #hash tags.
if(preg_match_all('/([!#@][^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/',$s,$match)) { if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
foreach($match[1] as $mtch) { foreach($matches[1] as $match) {
if(strstr($mtch,"]")) { if (strstr($match, ']')) {
// we might be inside a bbcode color tag - leave it alone // we might be inside a bbcode color tag - leave it alone
continue; continue;
} }
if(substr($mtch,-1,1) === '.') if (substr($match, -1, 1) === '.') {
$mtch = substr($mtch,0,-1); $match = substr($match,0,-1);
}
// ignore strictly numeric tags like #1 // ignore strictly numeric tags like #1
if((strpos($mtch,'#') === 0) && ctype_digit(substr($mtch,1))) if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
continue; continue;
}
// try not to catch url fragments // try not to catch url fragments
if(strpos($s,$mtch) && preg_match('/[a-zA-z0-9\/]/',substr($s,strpos($s,$mtch)-1,1))) if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
continue; continue;
$ret[] = $mtch; }
$ret[] = $match;
} }
} }
return $ret; return $ret;
}} }
// //