【问题标题】:Replace all urls with minified urls within a string containing mixed content用包含混合内容的字符串中的缩小 url 替换所有 url
【发布时间】:2020-12-28 19:10:15
【问题描述】:

我有带链接的字符串,我打算将链接提取到数组中,如下所示

$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";

preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match);

print_r($match[0]);

结果

Array ( 
[0] => http://google.com 
[1] => https://www.youtube.com/watch?v=K_m7NEDMrV0 
[2] => https://instagram.com/hellow/ 
) 

现在我将使用bit.ly API 函数gobitly() 来缩短以array 结尾的链接

foreach ($match[0] as $link){
    $links[] = gobitly($link);
}

$links[]的结果

Array ( 
[0] => http://t.com/1xx
[1] => http://t.com/z112
[2] => http://t.com/3431
) 

现在我想重建string 并替换指向新链接的链接,就像这样

$string = "The text you want to filter goes here. http://t.com/1xx, http://t.com/z112,http://t.com/3431";

【问题讨论】:

  • 请注意\w也匹配\d所以这部分[\w\d]+可以只是\w+[:punct:]也匹配一个逗号。
  • 请注意:我过去使用的至少两个第三方缩短服务已不复存在。彻底退休。如果我做了类似上述的事情,并且服务消失了,你最终会有点腐烂。如果您不记录原始网址,则您没有追索权。另一种方法是使用您自己的起酥油服务。

标签: php regex url preg-replace-callback


【解决方案1】:

您应该能够使用preg_replace_callback() 来操作匹配项并立即返回它们,而不是手动提取和替换它们(这需要更多的工作,我看不出有什么真正的原因?)。这是quick example

$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";

$replaced = preg_replace_callback('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', function($matches){
    // This function runs on each match
    $url = $matches[0];
    
    // Do bit.ly here. This is just an example
    $url = 'url/from/bit.ly/for:'.$url;
    
    // Return the new URL (which overwrites the match)
    return $url;
}, $string );

var_dump( $replaced );

这应该会给你一个预期的输出,比如:

string(191) "The text you want to filter goes here. url/from/bit.ly/for:http://google.com, url/from/bit.ly/for:https://www.youtube.com/watch?v=K_m7NEDMrV0,url/from/bit.ly/for:https://instagram.com/hellow/"

当然,不是我出于示例目的而使用的那种奇怪的串联,而是向 bit.ly 或任何你想要的 API 发出请求并使用该缩短的 URL

【讨论】:

    【解决方案2】:

    既然你知道要替换的url的key,你可以简单地循环然后使用str_replace将每个shorturl替换为原来的;

    <?php
    
    $string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";
    
    preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match);
    
    // Shorten array
    $short = [ 'http://t.com/1xx', 'http://t.com/z112', 'http://t.com/3431' ];
    
    // For each url
    foreach ($match[0] as $key => $value) {
        
        // Replace in original text
        $string = str_replace($value, $short[$key], $string);
    }
    
    echo $string;
    

    您要过滤的文本放在这里。 http://t.com/1xx, http://t.com/z112,http://t.com/3431

    Try it online!

    【讨论】:

      【解决方案3】:

      你需要preg_replace_callback:

      $newString = preg_replace_callback(
          '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#',
          function ($match) {
              // Use for debugging purposes
              // print_r($match);
      
              return gobitly($match[0]);    
          },
          $string
      );
      

      Fiddle,我用md5 代替了你的函数。

      【讨论】:

        猜你喜欢
        • 2015-11-22
        • 2011-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-04
        • 2014-12-19
        • 1970-01-01
        相关资源
        最近更新 更多