dourang8305 2014-04-29 21:11
浏览 46

如何在php中使用正则表达式验证后在数组中存储元素

I have a form in which will be inserted text with the web addresses. And i already have regular expression validation rule t search for web address. This rule will replace web links to another text. But my task is to write a function for saving all of this addresses to array. How can i save all of this addresses to array?

This is my code that i have for now:

<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>

  • 写回答

3条回答 默认 最新

  • du_1993 2014-04-29 21:15
    关注

    You can store anything into an array by just using the push function like so:

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

    or through:

    $array[] = webCheck($webtext);
    

    which is practically the same for you but considered a lot faster

    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名