如何在 PHP 中将可能的 URI 与其他内容分开?
检查字符串是否为单个 URL 或文本(可能包含 url)的最简单、最快的方法是什么?
可能的情况:
// successful scenario
$example[] = 'http://sub-domain.my-domain.com/folder/file.php?some=param';
// successful scenario
$example[] = '/assets/scripts/jquery.min.js?v=1.4';
// successful scenario
$example[] = 'jquery.min.js';
// this scenario should fail validation
$example[] = "http://www.domain.com welcome text\n and some other http://www.domain.com";
// this scenario should fail validation
$example[] = "scriptVar=50;";
我尝试使用本机 php 函数,如 parse_url、filter_var,但它们都没有按预期工作。
更新 1
为了更清楚地说明,我尝试将可能的 URI 与将作为 DOM 元素插入的脚本内容分开。所有网址都将作为 SRC 属性并作为内容,例如:
<script type="text/javascript" src="{$string}"></script>
<script type="text/javascript">{$string}</script>
UPDATE 2 通过分析可能的内容,我得出结论,包含空格字符或分号的字符串意味着该字符串不能是 URI,我认为这种模式可以解决我的问题:
preg_match('/[\s]|[;]/', $string);
它会覆盖所有可能的 javascript/css 代码吗?
What is the simplest and fastest way to check if string is single URL or TEXT (that might contain urls)
possible scenarios:
// successful scenario
$example[] = 'http://sub-domain.my-domain.com/folder/file.php?some=param';
// successful scenario
$example[] = '/assets/scripts/jquery.min.js?v=1.4';
// successful scenario
$example[] = 'jquery.min.js';
// this scenario should fail validation
$example[] = "http://www.domain.com welcome text\n and some other http://www.domain.com";
// this scenario should fail validation
$example[] = "scriptVar=50;";
I have tried to use native php functions like parse_url, filter_var but non of them work as expected.
UPDATE 1
To make it more clear, I'm trying to separate possible URI from script content that would be inserted as DOM element. All urls would go as SRC attribute and rest as content, example:
<script type="text/javascript" src="{$string}"></script>
<script type="text/javascript">{$string}</script>
UPDATE 2
By analysing possible content I come to conclusion that string containing white space character or semicolon mean that string could not be URI, I presume that this pattern could solve my problem:
preg_match('/[\s]|[;]/', $string);
would it cover all possible javascript/css code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将产生:
更新:
阅读您的上次更新后。如果你想解析 HTML。使用 DOM 解析器,例如:
http://simplehtmldom.sourceforge.net/
示例:
会输出类似( HTML 已删除):
This would produce:
Update:
After reading your last update. If you want to parse HTML. Use a DOM-parser like:
http://simplehtmldom.sourceforge.net/
Example:
Would output something like(HTML stripped):
如果传递的文本是 URL,则此函数将返回 true。它基于 SO 上看到的正则表达式。
您可以在这里尝试:http://www.exorithm.com/algorithm/view/validate_url
编辑 作为对评论的回应,此函数将验证 URL 片段,例如 /index.php 或 index.php
(请注意,空字符串是有效的,因此您可能需要一个特殊情况)
This function will return true if the passed text is an URL. It is based on a regex seen here on SO.
You can try it here: http://www.exorithm.com/algorithm/view/validate_url
EDIT in response to comment, this function will validate URL fragments like /index.php or index.php
(note that the empty string is valid, so you may want a special case for that)
filter_var
应该对单个 URL 执行您想要的操作:filter_var
should do what you want for a single URL: