gpt4 book ai didi

php - ZF2 : How to make Zend\Validator\Hostname accept url strings with and without http://or https://

转载 作者:行者123 更新时间:2023-12-04 15:55:01 24 4
gpt4 key购买 nike

我使用 Zend\Validator\Hostname 来验证包含 url 的输入字符串。问题是它只接受 mydomain.com 类型的 URL,而不接受带有 http://或 https://协议(protocol)前缀的 URL。实现所需行为的最佳方式是什么?

$inputFilter->add($factory->createInput(array(
'name' => 'shop_link',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
array(
'name' => 'Hostname',
'options' => array(
'allow' => \Zend\Validator\Hostname::ALLOW_DNS, // Allow these hostnames
'useIdnCheck' => true, // Check IDN domains
'useTldCheck' => true, // Check TLD elements
'ipValidator' => null, // IP validator to use
),
),
),
)));

谢谢

最佳答案

我没有 50 的声誉来评论 Sam 的回答,但是 Hostname::ALLOW_URI 不起作用。一年前没用 - https://stackoverflow.com/a/13902180/822947 .如果您查看源代码,则不应该工作。奇怪的是它有两个赞成票。

$validator = new \Zend\Validator\Hostname();
$validator->setAllow($validator::ALLOW_DNS | $validator::ALLOW_URI);
var_dump($validator->isValid('http://domain.com')); // false!

关于php - ZF2 : How to make Zend\Validator\Hostname accept url strings with and without http://or https://,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com