如何在最接近子字符串的字符串中找到 url(使用 stripos 后)? (PHP)
我正在使用 html 字符串并尝试找到最接近子字符串的 url。
if (stripos($theemailmessage,'substring') !== FALSE )
{
$indicatornumber = '1';
}
所以 stripos() 应该给我这个子字符串在字符串中的位置。我将如何从这里开始搜索 url 中的值?我假设这将是遍历字符串位置寻找 http://
,但我真的不确定我应该使用哪个函数。
我正在搜索的文档中有很多 URL,我正在搜索最接近字符串位置的 URL。实际上,我想先搜索看看字符串是否在锚标记内,但我想我应该先学习如何搜索最近的 url,然后从那里进行优化。
I'm working with an html string and trying to find the closest url in position to the substring.
if (stripos($theemailmessage,'substring') !== FALSE )
{
$indicatornumber = '1';
}
So stripos() should give me the position of this substring inside the string. How would I go about searching for values within a url from here? I'm assuming it would be something traversing the string positions looking for http://
, but I'm really not sure which function I should be using.
There are many URLs in the document that I am searching for, I'm searching for the one closest to the string position. Actually, I want to search to see if the string is inside an anchor tag first, but I figured I'd start by learning how to search for the closest url, and then refine from there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西吗?
Something like this?
您有多种选择来实现这一点。使用 preg_replace 函数或使用 parse_url 函数。
you have several options to implement this. use preg_replace function or use parse_url function.
祝你好运,找到一个匹配 URL 的正则表达式,但假设你有一个。
然后,
查看
$matches
,您将获得每个 URL 及其位置。迭代这些以找到最接近您的子字符串位置的值。确保考虑到匹配项和子字符串的长度。Good luck finding a regexp to match URLs, but suppose you have one.
Then,
Take a look in
$matches
and you will have every URL plus its position. Iterate over these to find which is closest to your substring position. Make sure that you account for the length of the matches and your substring.