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

Posted

tags:

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

good for parsing a message for urls so you can process them for character length when displayed on screen
  1. /**
  2.   Returns an array containing each of the sub-strings from text that
  3.   are between openingMarker and closingMarker. The text from
  4.   openingMarker and closingMarker are not included in the result.
  5.   This function does not support nesting of markers.
  6.   */
  7. function returnSubstrings($text, $openingMarker, $closingMarker) {
  8. $openingMarkerLength = strlen($openingMarker);
  9. $closingMarkerLength = strlen($closingMarker);
  10.  
  11. $result = array();
  12. $position = 0;
  13. while (($position = strpos($text, $openingMarker, $position)) !== false) {
  14. $position += $openingMarkerLength;
  15. if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) {
  16. $result[] = substr($text, $position, $closingMarkerPosition - $position);
  17. $position = $closingMarkerPosition + $closingMarkerLength;
  18. }
  19. }
  20. return $result;
  21. }
  22. $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>"
  23.  
  24. $urls = returnSubstrings($msg,'">','</a>');
  25.  
  26. print_r($urls);
  27.  
  28. // array (
  29. // 0 => 'http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N'
  30. // )
  31. //

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

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

使用 JavaScript/jQuery 将 URL 保存在锚标记中?

css:在锚标记上添加背景图像 URL

如何使用 querySelector 在锚标记的 href 属性末尾定位哈希?

用于将URL自动转换为可单击的超链接(锚定标记)的函数

URL的PHP​​和哈希/片段部分