Php 我是如何获得preg_match_all()的;从URL

Php 我是如何获得preg_match_all()的;从URL,php,Php,这是我的URL: <a href="http://sbayjai.com/forum/index.php?topic=67433.0">test_curl</a> 谢谢您的帮助。您可以使用以下正则表达式: $text = '<a href="http://sbayjai.com/forum/index.php?topic=67433.0">test_curl</a>'; $regex = '<a\s[^>]*href=(\"??)([

这是我的URL:

<a href="http://sbayjai.com/forum/index.php?topic=67433.0">test_curl</a>

谢谢您的帮助。

您可以使用以下正则表达式:

$text = '<a href="http://sbayjai.com/forum/index.php?topic=67433.0">test_curl</a>';
$regex = '<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>';

if(preg_match_all("/$regex/siU", $text, $matches)) {
  // urls
  var_dump($matches[2]);
  // link text
  var_dump($matches[3]);
}

如果您有兴趣了解正则表达式的工作原理,您可以在这里找到所有详细信息的非常好的解释。

谢谢,我会尝试它。您必须使用正确的正则表达式
/]*href=(\“?)([^\”>]*?)\\1[^>]*(.*)/
不是
/\test\u curl\/
对不起,我没有告诉您,我首先找到了“test\u curl”是一个主题,稍后获取url。两件事。1) 使用此表达式匹配
]*href=(\“?)([^\“>]*?)\\1[^>]*>(test\u curl)
。请注意,url标题在括号中!2) 在我的代码中添加模式修饰符,即
/…/siU
$text = '<a href="http://sbayjai.com/forum/index.php?topic=67433.0">test_curl</a>';
$regex = '<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>';

if(preg_match_all("/$regex/siU", $text, $matches)) {
  // urls
  var_dump($matches[2]);
  // link text
  var_dump($matches[3]);
}
array(1) {
  [0]=>
  string(48) "http://sbayjai.com/forum/index.php?topic=67433.0"
}
array(1) {
  [0]=>
  string(9) "test_curl"
}