PHP

php и регулярные выражения? Как выделить ссылки?

Код HTML
<br>[04:17:08]  <B>Stas => </B> : www.google.ru<br>[04:17:19]  <B>Stas => </B> : <a href="http://www.google.com" target="_blank" rel="nofollow">http://www.google.com</a><br>[04:17:30]  <B>Stas => </B> : google.ru


тут уже есть ссылка в html

Выполним код
Код PHP
<?php
$preg_autolinks = array(
    'pattern' => array(
        "'[\w\+]+://[A-z0-9\.\?\+\-/_=&%#:;]+[\w/=]+'si",
        "'([^/])(www\.[A-z0-9\.\?\+\-/_=&%#:;]+[\w/=]+)'si",
    ),
    'replacement' => array(
        '<a href="$0" target="_blank" rel="nofollow">$0</a>',
        '$1<a href="http://$2" target="_blank" rel="nofollow">$2</a>',
    ));
$search = $preg_autolinks['pattern'];
$replace = $preg_autolinks['replacement'];
$text = '<br>[04:17:08]  <B>Stas => </B> : www.google.ru<br>[04:17:19]  <B>Stas => </B> : <a href="http://www.google.com" target="_blank" rel="nofollow">http://www.google.com</a><br>[04:17:30]  <B>Stas => </B> : google.ru';
$text = preg_replace($search, $replace, $text);
#$text=preg_replace($search, $replace, $text);
print $text;
?>

Всё ок, кроме той ссылки которую уже написали в формате html...
Теперь раскомментируем закомментированную строку.
Вот и получаем то что у тебя в чате.
№ 2
<?php 

$text = '<br>[04:17:08]  <B>Stas => </B> : www.google.ru<br>[04:17:19]  <B>Stas => </B> : www.google.com<br>[04:17:30]  <B>Stas => </B> : 

/**

* Replace links in text with html links

*

* @param string $text

* @return string

*/

function auto_link_text($text)

{

$pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';

$callback = create_function('$matches', '

$url = array_shift($matches);

$url_parts = parse_url($url);



$text = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);

$text = preg_replace("/^www./", "", $text);



$last = -(strlen(strrchr($text, "/"))) + 1;

if ($last < 0) {

$text = substr($text, 0, $last) . "…";

}



return sprintf(\'<a rel="nofollow" href="%s">%s</a>\', $url, $text);

');



return preg_replace_callback($pattern, $callback, $text);

}

echo auto_link_text($text);

?>

№3
function auto_link_text($text) {
    $pattern  = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
    return preg_replace_callback($pattern, 'auto_link_text_callback', $text);
}

function auto_link_text_callback($matches) {
    $max_url_length = 50;
    $max_depth_if_over_length = 2;
    $ellipsis = '…';

    $url_full = $matches[0];
    $url_short = '';

    if (strlen($url_full) > $max_url_length) {
        $parts = parse_url($url_full);
        $url_short = $parts['scheme'] . '://' . preg_replace('/^www\./', '', $parts['host']) . '/';

        $path_components = explode('/', trim($parts['path'], '/'));
        foreach ($path_components as $dir) {
            $url_string_components[] = $dir . '/';
        }

        if (!empty($parts['query'])) {
            $url_string_components[] = '?' . $parts['query'];
        }

        if (!empty($parts['fragment'])) {
            $url_string_components[] = '#' . $parts['fragment'];
        }

        for ($k = 0; $k < count($url_string_components); $k++) {
            $curr_component = $url_string_components[$k];
            if ($k >= $max_depth_if_over_length || strlen($url_short) + strlen($curr_component) > $max_url_length) {
                if ($k == 0 && strlen($url_short) < $max_url_length) {
                    // Always show a portion of first directory
                    $url_short .= substr($curr_component, 0, $max_url_length - strlen($url_short));
                }
                $url_short .= $ellipsis;
                break;
            }
            $url_short .= $curr_component;
        }

    } else {
        $url_short = $url_full;
    }

    return "<a rel=\"nofollow\" href=\"$url_full\">$url_short</a>";
}

function auto_link_text($text) {
    $pattern  = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
    return preg_replace_callback($pattern, 'auto_link_text_callback', $text);
}

function auto_link_text_callback($matches) {
    $max_url_length = 50;
    $max_depth_if_over_length = 2;
    $ellipsis = '…';

    $url_full = $matches[0];
    $url_short = '';

    if (strlen($url_full) > $max_url_length) {
        $parts = parse_url($url_full);
        $url_short = $parts['scheme'] . '://' . preg_replace('/^www\./', '', $parts['host']) . '/';

        $path_components = explode('/', trim($parts['path'], '/'));
        foreach ($path_components as $dir) {
            $url_string_components[] = $dir . '/';
        }

        if (!empty($parts['query'])) {
            $url_string_components[] = '?' . $parts['query'];
        }

        if (!empty($parts['fragment'])) {
            $url_string_components[] = '#' . $parts['fragment'];
        }

        for ($k = 0; $k < count($url_string_components); $k++) {
            $curr_component = $url_string_components[$k];
            if ($k >= $max_depth_if_over_length || strlen($url_short) + strlen($curr_component) > $max_url_length) {
                if ($k == 0 && strlen($url_short) < $max_url_length) {
                    // Always show a portion of first directory
                    $url_short .= substr($curr_component, 0, $max_url_length - strlen($url_short));
                }
                $url_short .= $ellipsis;
                break;
            }
            $url_short .= $curr_component;
        }

    } else {
        $url_short = $url_full;
    }

    return "<a rel=\"nofollow\" href=\"$url_full\">$url_short</a>";
}
Добавлено: 19 Августа 2013 02:54:54 Добавил: Андрей Ковальчук Нравится 0
Добавить
Комментарии:
Нету комментариев для вывода...