我在制作或找到一个可以转动的脚本时遇到麻烦
[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
)
是的,我已经尝试了其他一些过滤器,然后是这里描述的。
<?php
function PregGet( $text, $regex ) {
preg_match_all( $regex, $text, $matches );
return $matches[2];
}
function PregReplace( $text, $regex, $replace ) {
return preg_replace( $regex, $replace, $text );
}
$text = '[link]https://google.com[/link] <br>[link]https://yahoo.com[/link]';
$matches = PregGet( $text, '([(link)](.*?)[/(link)])' );
foreach ( $matches as $match ) {
$a = str_replace('[link]', '', str_replace('[/link]', '', $match))
$text = PregReplace( $text, '([(link)](' . $a . ')[/(link)])', '<a href="' . $a . '">' . $a . '</a>' );
}
你可以删除"[link]"和"[/link]",然后,获取实际的URL并构建链接:
<?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>
嗨,您可以在 [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
希望这对您有所帮助。