текст ссылки, проблема со ссылкой, которая не имеет https

Вы также можете использовать следующую функцию.

void tokenize(const string& str, vector<string>& tokens, const string& delimiters = ",")
{
  // Skip delimiters at beginning.
  string::size_type lastPos = str.find_first_not_of(delimiters, 0);

  // Find first non-delimiter.
  string::size_type pos = str.find_first_of(delimiters, lastPos);

  while (string::npos != pos || string::npos != lastPos) {
    // Found a token, add it to the vector.
    tokens.push_back(str.substr(lastPos, pos - lastPos));

    // Skip delimiters.
    lastPos = str.find_first_not_of(delimiters, pos);

    // Find next non-delimiter.
    pos = str.find_first_of(delimiters, lastPos);
  }
}
0
задан Moishey Schwartz 18 January 2019 в 13:42
поделиться

1 ответ

Используйте вместо этого preg_replace_callback, и вы сможете запросить совпадение, чтобы узнать, нужно ли вам добавить протокол.

function toLink($titulo) {
    $url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i'; 
    $titulo = preg_replace_callback($url, function($matches) {
        $url = $matches[0];
        if (!preg_match('/^https?:\/\//', $url)) $url = 'http://'.$matches[0];
        '<a href="'.$url.'" target="_blank" title="'.$url.'">'.$url.'</a>';
    }, $titulo);
    return $titulo;
}
0
ответ дан Utkanos 18 January 2019 в 13:42
поделиться
Другие вопросы по тегам:

Похожие вопросы: