XF 2.1 function to regex thread_id

Robert9

Well-known member
/threads/title-with-minus.58321/

I have a link to a thread and need the thread_id. I can regex this, but i am shure that XF has a function to do that.
May someone help me with the name, please?
 
.if the function is outside a controller, overwrite $this->router() to \XF::app()->router().
PHP:
public function getThreadIdFromUrl($url)
{
    $format = ':int<thread_id,title>/:page';
    $suffix = substr($url, strpos($url, 'threads/') + 8);

    $matchRegex = $this->router()->generateMatchRegexInner($format, '#');
    if (!preg_match('#^' . $matchRegex . '#i', $suffix, $textMatch))
    {
        return false;
    }

    $matchText = $textMatch[0];
    $trail = substr($suffix, strlen($matchText));

    $action = isset($textMatch['_action']) ? $textMatch['_action'] : '';
    $params = [];

    unset($textMatch['_action']);

    foreach ($textMatch AS $key => $value)
    {
        if (is_string($key) && strlen($value))
        {
            $params[$key] = $value;
        }
    }

    return isset($params['thread_id']) ? (int)$params['thread_id'] : null;
}

Example: $this->getThreadIdFromUrl('https://xenforo.com/community/threads/function-to-regex-thread_id.171454') will return 171454.
 
Thank you, at the moment i use this one:

Code:
if (preg_match('#^[URL]https://www.forum.com/threads/([A-Za-zäöüß0-9%-]+).([0-9]+)/$#[/URL]'), $link, $match)) {}
 
Back
Top Bottom