【问题标题】:Find and replace all URL'S that is between [link] and [/link] with hyperlinks查找并用超链接替换 ​​[link] 和 [/link] 之间的所有 URL
【发布时间】: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
)

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

【问题讨论】:

    标签: php url replace hyperlink


    【解决方案1】:
    <?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>' );
    }
    

    【讨论】:

      【解决方案2】:

      您可以删除“[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>
      

      【讨论】:

        【解决方案3】:

        您好,您可以在 [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
        

        希望对你有所帮助。

        【讨论】:

          猜你喜欢
          • 2012-01-22
          • 2017-06-12
          • 2021-10-19
          • 2011-09-19
          • 2013-07-12
          • 2015-10-16
          • 2018-03-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多