php正则获取a标签连接
时间: 2023-08-22 16:05:56 浏览: 43
你可以使用以下正则表达式来获取a标签的连接:
```
/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU
```
然后在 PHP 中使用 preg_match_all() 函数来匹配所有的 a 标签连接,代码如下:
```php
preg_match_all('/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $html, $matches);
$links = $matches[2]; // 获取所有的连接
```
其中,$html 是包含 a 标签的 HTML 字符串,$links 就是所有的 a 标签连接。
相关问题
正则表达式获取a标签
可以使用以下正则表达式来获取a标签:
```python
import re
html = '<a href="https://www.example.com">Example</a>'
pattern = r'<a.*?href="(.*?)".*?>(.*?)</a>'
match = re.search(pattern, html)
if match:
url = match.group(1)
text = match.group(2)
print(url, text)
```
输出:
```
https://www.example.com Example
```
js正则表达式获取a标签
可以使用正则表达式来获取a标签,具体的正则表达式如下所示:
```javascript
/<a[^>]*href=['"]([^'"]+)['"][^>]*>(.*?)<\/a>/gi
```
其中,`[^>]*`表示匹配除了`>`以外的任意字符,`[^'"]+`表示匹配除了单引号和双引号以外的任意字符,`.*?`表示匹配任意字符,但是尽可能少地匹配。
下面是一个示例代码:
```javascript
const html = '<a href="https://www.baidu.com">百度</a><a href="https://www.google.com">谷歌</a>';
const regex = /<a[^>]*href=['"]([^'"]+)['"][^>]*>(.*?)<\/a>/gi;
let match;
while ((match = regex.exec(html)) !== null) {
console.log(match[1], match[2]);
}
```
输出结果为:
```
https://www.baidu.com 百度
https://www.google.com 谷歌
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)