使用超链接查找并替换[link]和[/ link]之间的所有URL&#S;

时间:2018-04-06 09:39:48

标签: php url replace hyperlink

我无法制作或找到转动的脚本

[link]https://google.com[/link]

进入

<a href="https://google.com">https://google.com</a>

等等。 我试过这些东西......

$message = str_replace("[link]", "<a href=\"", $message);
$message = str_replace("[/link]", "\" target=\"_blank\">LINK</a>", $message);


preg_match('@((https?://)?([-\\w]+\\.[-\\w\\.]+)+\\w(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)*)@', $message, $url);

foreach($url as $link){
  $between = get_string_between($fullstring, '[link]', '[/link]');
  $message = str_replace("[link]", "<a href=\"", $message);
  $message = str_replace("[/link]", "\" target=\"_blank\">$between</a>", $message);
}


function get_string_between($string, $start, $end){
   $string = ' ' . $string;
   $ini = strpos($string, $start);
   if ($ini == 0) return '';
   $ini += strlen($start);
   $len = strpos($string, $end, $ini) - $ini;
   return substr($string, $ini, $len);
}

preg_match('@((https?://)?([-\\w]+\\.[-\\w\\.]+)+\\w(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)*)@', $message, $url);

foreach($url as $link){
  $message = str_replace("[link]", "<a href=\"", $message);
  $message = str_replace("[/link]", "\" target=\"_blank\">LINK</a>", $message);
}

但错误是输出不是

<a href="https://google.com">https://google.com</a>

<a href="https://google.com">LINK</a>

或每次变量之间只有相同的$

<a href="https://gyazo.com/f8df70551ddf79c5f5a841b904324db7">https://gyazo.com/f8df70551ddf79c5f5a841b904324db7</a>
<a href="https://google.com">https://gyazo.com/f8df70551ddf79c5f5a841b904324db7</a>

我用preg_match()尝试的代码有很多随机的东西。

Array(
  [0] => https://gyazo.com/f8df70551ddf79c5f5a841b904324db7
  [1] => https://gyazo.com/f8df70551ddf79c5f5a841b904324db7
  [2] => https://
  [3] => gyazo.co
  [4] => 
  [5] => /f8df70551ddf79c5f5a841b904324db7
  [6] => f8df70551ddf79c5f5a841b904324db7
)

是的,我尝试了其他一些过滤器,然后在这里描述。

3 个答案:

答案 0 :(得分:0)

您可以删除“[link]”和“[/ link]”,之后,获取实际网址并构建链接:

<?php

$links = array(
    '[link]https://google.com[/link]',
    '[link]https://gyazo.com/f8df70551ddf79c5f5a841b904324db7[/link]'
);

foreach ($links as $link){
    $link = str_replace('[link]', '', $link);
    $link = str_replace('[/link]', '', $link);
    $message = '<a href="'. $link . '">' . $link . '</a>';

    echo $message . "\n";
}

输出:

<a href="https://google.com">https://google.com</a>
<a href="https://gyazo.com/f8df70551ddf79c5f5a841b904324db7">https://gyazo.com/f8df70551ddf79c5f5a841b904324db7</a>

答案 1 :(得分:0)

您好,您可以在 [link] [/ link] 块之间找到字符串。看到这个。

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

$message = "[link]https://google.com[/link]";
$url = get_string_between($message, '[link]', '[/link]');
$message = str_replace("[link]", "<a href=\"", $message);
$message = str_replace("[/link]", "\" target=\"_blank\">".$url."</a>", $message);

echo $message

希望这可以帮助你。

答案 2 :(得分:0)

from django.urls reverse_lazy
​
# change password urls
path('password-change/', auth_views.PasswordChangeView.as_view(success_url=reverse_lazy('account:password_change_done')), name='password_change'),