在php中验证url

时间:2011-02-22 10:09:55

标签: php

我有phpmydatagrid,其中有单独的按钮可供用户输入,删除和编辑数据。有一列用于输入网址,我想验证它(例如:我希望所有用户输入完整的URL与http或https请求,它不应该只是www.something.com),我想知道究竟输入语法的位置 我附上了phpmydatagrid的编码。

phpMyDatagrid - 示例文件

/* Include class file */
include ("phpmydatagrid.class.php");

/* Create object */
$objGrid = new datagrid;

/* Define the "FORM" will be named employee and Must be 
   created by the grid script */
$objGrid -> form('employee', true);

/* Connect with the database */
$objGrid -> conectadb("localhost", "root", "mysql", "probe_config");

/* Select the table to use */
$objGrid -> tabla ("measurementurl");

$objGrid -> buttons(addbtn,updbtn,delbtn,false);




$objGrid -> datarows(5);

$objGrid -> paginationmode('mixed');


/* Define fields to show */

  $objGrid -> FormatColumn("id", "ID", 30, 30, 0, "150", "left");
$objGrid -> FormatColumn("name", "NAME", 30, 30, 0, "50", "right");
$objGrid -> FormatColumn("url", "url", 90, 90, 0, "90", "left");
    $objGrid -> FormatColumn("comment", "COMMENT", 30, 30, 0, "150", "left");


$objGrid -> keyfield("id");



/* The setHeader function MUST be set between the <HEAD> and </HEAD> 
   to correctly set the CSS and JS parameters */
$objGrid -> setHeader();

&GT;

$objGrid -> ajax("silent");

/* draw the grid */
    $objGrid -> grid();


/* Disconnect from database */
$objGrid -> desconectar();

&GT;

home

2 个答案:

答案 0 :(得分:0)

PHP代码:

function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

使用功能:

PHP代码:

if(!isValidURL($fldbanner_url))
{
$errMsg .= "* Please enter valid URL including http://<br>";
}

答案 1 :(得分:0)

如果您安装了过滤器扩展程序,请使用filter_var功能:

if(filter_var($url, FILTER_VALIDATE_URL)) {
  //Valid URL
} else {
  //Invalid URL
}