【发布时间】:2018-09-16 07:18:26
【问题描述】:
我在制作或找到一个脚本时遇到了麻烦
[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>
或者每次都使用相同的 $between 变量
<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
)
是的,我已经尝试了其他一些过滤器,然后是这里描述的内容。
【问题讨论】: