簡體   English   中英

從正則表達式捕獲組獲取所有可能的匹配項

[英]Getting all possible matches from a regex capture group

考慮以下正則表達式:

/\<form.+?((action|id|method|name)=(\"|\')(.*?)?(\"|\')).*?\>/i

足以捕獲諸如<form>基本內容,也應捕獲諸如<form action="post.php" method="post" name="form1">以及上面表達式中列出的這四個屬性的其他各種組合。

之所以選擇此表達式而不是基本的/\\<form.*?\\>/i是因為我想從捕獲組2和4中獲取值(屬性名稱和屬性值)。 但是,當我在類似於上面復雜的表單元素上運行此表達式時,它將僅返回actionpost.php 我希望它返回匹配項數組。

這是一些示例代碼:

<?php
    $string = '<form action="post.php" method="post" name="form1">';
    preg_match_all('/\<form.+?((action|id|method|name)=(\"|\')(.*?)?(\"|\')).*?\>/i', $string, $forms);
    print_r($forms);
?>

如果出於演示目的在命令行中運行此命令,則輸出如下:

c:\Users\Aaron\Desktop>php test.php
Array
(
    [0] => Array
        (
            [0] => <form action="post.php" method="post" name="form1">
        )

    [1] => Array
        (
            [0] => action="post.php"
        )

    [2] => Array
        (
            [0] => action
        )

    [3] => Array
        (
            [0] => "
        )

    [4] => Array
        (
            [0] => post.php
        )

    [5] => Array
        (
            [0] => "
        )

)

我想要的輸出將是這樣的:

c:\Users\Aaron\Desktop>php test.php
Array
(
    [0] => Array
        (
            [0] => <form action="post.php" method="post" name="form1">
            [1] => <form action="post.php" method="post" name="form1">
            [2] => <form action="post.php" method="post" name="form1">
        )

    [1] => Array
        (
            [0] => action="post.php"
            [1] => method="post"
            [2] => name="form1"
        )

    [2] => Array
        (
            [0] => action
            [1] => method
            [2] => name
        )

    [3] => Array
        (
            [0] => "
            [1] => "
            [2] => "
        )

    [4] => Array
        (
            [0] => post.php
            [1] => post
            [2] => form1
        )

    [5] => Array
        (
            [0] => "
            [1] => "
            [2] => "
        )

)

目前,我可以通過找到form元素並針對需要搜索的多個屬性多次運行表達式來解決此問題。 這是代碼 但是我不禁想到必須有一種更簡單的方法?

所以問題是:我可以從捕獲組中返回所有匹配項,而不僅僅是第一個匹配項嗎?

提前致謝。

我真誠的建議是不要使用正則表達式來處理( HTML ),而只需使用DOM分析器即可。

編碼..

<?php
$string = '<form action="post.php" method="post" name="form1">';
$dom = new DOMDocument;
$dom->loadHTML($string);
foreach ($dom->getElementsByTagName('form') as $ftag) {
    if ($ftag->hasAttributes()) {
        foreach ($ftag->attributes as $attribute) {
            $attrib[$attribute->nodeName] = $attribute->nodeValue;
        }
    }
}
print_r($attrib);

輸出:

Array
(
    [action] => post.php
    [method] => post
    [name] => form1
)

您必須首先找到一個表單元素。

<?php
 $string = '<form action="post.php" method="post" name="form1">';
 preg_match_all('/\<form+?\>/i', $string, $forms);

然后在里面應用正則表達式:

 foreach($form in $forms){
  preg_match_all('/((action|id|method|name)=(("[^"]*")|(\'[^\']*\'))/i',$form[0],$attrs);
 }
 $form = array_merge($form,$attrs);
 print_r($forms);
?>

我沒有設備可以嘗試運行。 希望能做到:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM