【问题标题】:how to store elements in array after validation with regular expression in php如何在php中使用正则表达式验证后将元素存储在数组中
【发布时间】:2014-04-29 21:11:14
【问题描述】:

我有一个表格,其中将插入带有网址的文本。而且我已经有正则表达式验证规则来搜索网址。此规则将替换指向另一个文本的 Web 链接。但我的任务是编写一个函数来将所有这些地址保存到数组中。我怎样才能将所有这些地址保存到数组中?

这是我现在拥有的代码:

<html>
<head>
    <meta charset="UTF-8">
    <title>Expressions: Find web address in text</title>
</head>
<body>
    <?php

    /*--------Functions---------*/
    function webCheck($webtext){
        global $web_check;
        $web_check = "/((http:\/\/www\.)|(http:\/\/)|(www\.))([a-z0-9]+([a-z0-9-]*[a-z0-9]+)*\.)+[a-z]+/i";
            return preg_replace($web_check, "<b>was weblink here</b>", $webtext);
        }

    /*--------End of Functions----------*/



    if(isset($_POST["webadd"])){
        $webtext = $_POST["webtext"]; 

        if (!empty($webtext)){
            echo webCheck($webtext);
        }else{
            echo "Feald cannot be empty. Please enter some text.";
        }

    }  else {
        echo "Please enter text in text area.";
    }
    ?>

    <form action="expression3.php" method="POST">
        <table>
            <tr>
                <td>Find web address in text</td>
            </tr>
            <tr>
                <td> <textarea name="webtext" cols="50" rows="10"></textarea></td>
            </tr>
            <tr>
                <td><input type="submit" name="webadd" value="Find Web Address!" /></td>
            </tr>
        </table>
    </form>

</body>

【问题讨论】:

    标签: php arrays regex


    【解决方案1】:

    您可以使用push 函数将任何内容存储到数组中,如下所示:

    $my_links_array = array();
    array_push($my_links_array, webCheck($webtext));
    

    或通过:

    $array[] = webCheck($webtext);
    

    这对你来说几乎是一样的,但被认为要快得多

    【讨论】:

      【解决方案2】:

      您可以使用 preg_replace_callback 允许在闭包中执行代码:

      $myarr = array();
      return preg_replace_callback($web_check, 
                                   function ($m) use (&$myarr) {
                                       $myarr[] = $m[0];
                                       return '<b>was weblink here</b>';
                                   }, $webtext);
      

      请注意,要更改闭包内的变量,您需要通过引用传递它。

      【讨论】:

        【解决方案3】:

        试试..

        /*--------Functions---------*/
        $arrWebAddreses = array();
        function webCheck($webtext){
            global $web_check;
            $web_check = "/((http:\/\/www\.)|(http:\/\/)|(www\.))([a-z0-9]+([a-z0-9-]*[a-z0-9]+)*\.)+[a-z]+/i";
            $matches = array();
            preg_match($web_check, $webtext, $matches);
        
            if(count($matches)>0)
              $arrWebAdresses += $matches[0];        
        
            return preg_replace($web_check, "<b>was weblink here</b>", $webtext);
        }
        
        /*--------End of Functions----------*/
        

        【讨论】:

          猜你喜欢
          • 2012-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-26
          • 1970-01-01
          • 2011-01-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多