PHP 使用php提取锚标记之间的URL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 使用php提取锚标记之间的URL相关的知识,希望对你有一定的参考价值。

/**
    Returns an array containing each of the sub-strings from text that
    are between openingMarker and closingMarker. The text from
    openingMarker and closingMarker are not included in the result.
    This function does not support nesting of markers.
  */
  function returnSubstrings($text, $openingMarker, $closingMarker) {
    $openingMarkerLength = strlen($openingMarker);
    $closingMarkerLength = strlen($closingMarker);

    $result = array();
    $position = 0;
    while (($position = strpos($text, $openingMarker, $position)) !== false) {
      $position += $openingMarkerLength;
      if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) {
        $result[] = substr($text, $position, $closingMarkerPosition - $position);
        $position = $closingMarkerPosition + $closingMarkerLength;
      }
    }
    return $result;
  }
$msg = "This is a string with a url in <a href="http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N">http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N</a>"

$urls = returnSubstrings($msg,'">','</a>');

print_r($urls);

  // array (
  //   0 => 'http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N'
  // )
 //

以上是关于PHP 使用php提取锚标记之间的URL的主要内容,如果未能解决你的问题,请参考以下文章

使用php在锚定标记之间提取url

PHP 自动将URL转换为可点击超链接(锚标记)的功能

PHP PHP:使用正则表达式从简介中删除锚点和其他标记

[我想使用foreach循环php中的ajax来获取被点击行的UID,但获取所有UID

使用正则表达式匹配 PHP 中的 URL 模式

PHP:用正则表达式从简介中删除锚和其他标记