簡體   English   中英

如何在PHP中使用正則表達式進行匹配

[英]How to use regular expression to match in PHP

我在HTML頁面上有一些文本,我想使用正則表達式進行匹配,但是我遇到了麻煩。

<tr id="part1AModel.errors">
             <td colspan="5">  
                <h3><font color="red">Validation Error</font></h3>You must correct the following error(s) before proceeding:
                <ul>

                        <li><font color="red">The requested effective date entered is not an expedited date. If an expedite is required, please either leave the default effective date or change the effective date to a date that is less than 31 days. If the effective date entered is the date you want, please be sure to select "No"Â for Requested Expedite Treatment and uncheck the Earliest Effective Date checkbox.<br/></font> </li>

                </ul>  
                For assistance, please contact the Help Desk.<hr> 
             </td></tr> 

以上是我的代碼,我想嘗試與

<tr id="part1AModel.errors"> </tr>

因此,如果它存在,我可以將其返回,但無法弄清楚它的生命周期語法。 我用了

/<tr id=\"part1AModel.errors\">(.*?)<\/tr>/

但不匹配。 我在這里想念什么?

默認情況下, . 點圖案匹配換行符以外的任何字符。 . 還匹配換行符,請使用/s PCRE_DOTALL選項。 點格式的PHP regexp參考中提到了這一點:

http://php.net/manual/en/regexp.reference.dot.php

在字符類之外,模式中的點與主題中的任何一個字符匹配,包括非打印字符,但(默認情況下)不與換行符匹配。 如果設置了PCRE_DOTALL選項,則點也匹配換行符。

您可以將其應用到您的示例中,如下所示:

/<tr id=\\"part1AModel\\.errors\\">(.*?)<\\/tr>/s

我還進行了以下更改:

  • 逃脫了. 匹配字面量而不是任何字符的句點。
  • 感動了? ()捕獲組之后,使捕獲可選,如下所示:

/<tr id=\\"part1AModel\\.errors\\">([\\s\\S]*)?<\\/tr>/

將此與preg_match一起使用將返回:

Array
(
[0] => Array
    (
        [0] => <tr id="part1AModel.errors">
         <td colspan="5">  
            <h3><font color="red">Validation Error</font></h3>You must correct the following error(s) before proceeding:
            <ul>

                    <li><font color="red">The requested effective date entered is not an expedited date. If an expedite is required, please either leave the default effective date or change the effective date to a date that is less than 31 days. If the effective date entered is the date you want, please be sure to select "No"Â for Requested Expedite Treatment and uncheck the Earliest Effective Date checkbox.<br/></font> </li>

            </ul>  
            For assistance, please contact the Help Desk.<hr> 
         </td></tr>
        [1] => 0
    )

[1] => Array
    (
        [0] => 
         <td colspan="5">  
            <h3><font color="red">Validation Error</font></h3>You must correct the following error(s) before proceeding:
            <ul>

                    <li><font color="red">The requested effective date entered is not an expedited date. If an expedite is required, please either leave the default effective date or change the effective date to a date that is less than 31 days. If the effective date entered is the date you want, please be sure to select "No"Â for Requested Expedite Treatment and uncheck the Earliest Effective Date checkbox.<br/></font> </li>

            </ul>  
            For assistance, please contact the Help Desk.<hr> 
         </td>
        [1] => 28
    )
)

暫無
暫無

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

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