PHP的插入preg_match_all数组 [英] php inserting preg_match_all array

查看:76
本文介绍了PHP的插入preg_match_all数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个代码,用于捕获POST中的所有url并将它们短接,然后将它们每个都插入mysql中的行中.....但是这里将所有url插入一行中?所以我怎样才能让它捕获第一个URL,然后将其插入数据库,然后返回第二个URL,并执行相同操作.

this is a code to catch all url in the POST and shorting them then insert each of them in row in mysql ..... but here it is inserting all the url in one row ?? so how can i make it catch the first url then insert it in database then go back for the second one and do the same..???

$urlinput=mysql_real_escape_string($_POST['url']); 
$pattren="/(http:\/\/)[a-zA-Z0-9]*\.[a-z]*(.*)|(www)\.[a-zA-Z0-9]*\.[com]*(.*)/";
preg_match_all( $pattren, $urlinput, $matches );
foreach($matches[0] as $match) {

$id=rand(10000,99999);
$shorturl=base_convert($id,20,36);
$sql = "insert into url values('$id','$match','$shorturl')";
mysql_query($sql,$con);
}

推荐答案

好吧,正则表达式中的(.*)"会在找到url的开头后匹配所有内容.因此,如果您希望在一个字符串中使用多个网址,或者用逗号分隔或不使用逗号,则它们将全部捆绑在一起.

well, "(.*)" in your regex matches everything after it has found the beginning of a url. so if you're expecting multiple urls in one string, or coma separated or what not, they will all be bundled together.

您需要能够先分割输入内容,然后在选择要插入的每个元素之前先验证每个元素是否为有效网址.

You need to be able to split your input first, and then validate that each element is a valid url before you pick it up for insertion.

这篇关于PHP的插入preg_match_all数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆