ホームページ > バックエンド開発 > PHPチュートリアル > 定期的なマッチングに関する質問です。アドバイスをお願いします。

定期的なマッチングに関する質問です。アドバイスをお願いします。

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
リリース: 2016-06-13 13:28:04
オリジナル
1091 人が閲覧しました

正则匹配问题 求指教
获取一个A标签的Href属性

被匹配的字符

连接1
连接2
连接3


可以没有引号 可以有双引号 单引号 但是不允许一侧有引号另一侧没有引号

PHP code

1

2

3

4

5

6

<!--

 

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

 

-->preg_match('/<a([\s\S]*)href=(["\']?)([\s\S]*)\1\sid([\s\S]*)</a>/i', $str, $matches)

ログイン後にコピー



这样怎么不行呢

------解决方案--------------------
PHP code

1

2

3

4

5

6

7

8

9

10

$str=<<<STR

<a class="a" href="http://www.baidu.com/" id="a1">连接1</a>

<a class="a" href='http://www.baidu.com/' id="a2">连接2</a>

<a class="a" href=http://www.baidu.com/ id="a3">连接3</a>

<a class="a" href="http://www.baidu.com/ id="a3">连接3</a>

STR;

 

preg_match_all('/<a[^>]+href=(["\']?)([^\s"\']+)\1[^>]*>/',$str,$matches);

print_r($matches[2]);

<br><font color="#e78608">------解决方案--------------------</font><br>

ログイン後にコピー
PHP code

1

2

3

4

5

6

7

8

$str=<<<html

<div class="a" href="http://www.baidu.com/" id="a1">连接1</a>

<div class="a" href='http://www.baidu.com/' id="a2">连接2</a>

<div class="a" href=http://www.baidu.com/ id="a3">连接3</a>

html;

preg_match_all('/href=(["\'])?([^\s"\']+)/is', $str, $matches) ;

print_r($matches);

<br><font color="#e78608">------解决方案--------------------</font><br>

ログイン後にコピー
PHP code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

[User:root Time:14:14:52 Path:/home/liangdong/php]$ php preg.php

Array

(

    [0] => Array

        (

            [0] => <div class="a" href="http://www.baidu.com/"

            [1] => <div class="a" href='http://www.baidu.com/'

            [2] => <div class="a" href=http://www.baidu.com/

        )

 

    [1] => Array

        (

            [0] => "

            [1] => '

            [2] =>

        )

 

    [2] => Array

        (

            [0] => http://www.baidu.com/

            [1] => http://www.baidu.com/

            [2] => http://www.baidu.com/

        )

 

)

[User:root Time:14:14:53 Path:/home/liangdong/php]$ cat preg.php

<?php

$str = <<<EOF

<div class="a" href="http://www.baidu.com/" id="a1">连接1</a>

<div class="a" href='http://www.baidu.com/' id="a2">连接2</a>

<div class="a" href=http://www.baidu.com/ id="a3">连接3</a>

EOF;

preg_match_all('/<div class="a" href=(["\']?)([^\s\'"]*)\1/is', $str, $matches);

print_r($matches);

?> <div class="clear">

                  

               

               

         

            </div>

ログイン後にコピー
関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート