如何在最接近子字符串的字符串中找到 url(使用 stripos 后)? (PHP)

发布于 2024-10-16 02:03:40 字数 384 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒芜了季节 2024-10-23 02:03:40

像这样的东西吗?

preg_match('/http\:\/\/[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(\/\S*)?/', $str, $url);

回显$url[0];

Something like this?

preg_match('/http\:\/\/[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(\/\S*)?/',$str, $url);

echo $url[0];

謌踐踏愛綪 2024-10-23 02:03:40

您有多种选择来实现这一点。使用 preg_replace 函数或使用 parse_url 函数。

$tempText = "hello.. how are you?? are you keeping well?? please checkout this url http://www.youtube.com/watch?v=ehuwoGVLyhg&feature=topvideos";

print_r(parse_url($tempText,PHP_URL_PATH));

you have several options to implement this. use preg_replace function or use parse_url function.

$tempText = "hello.. how are you?? are you keeping well?? please checkout this url http://www.youtube.com/watch?v=ehuwoGVLyhg&feature=topvideos";

print_r(parse_url($tempText,PHP_URL_PATH));
走过海棠暮 2024-10-23 02:03:40

祝你好运,找到一个匹配 URL 的正则表达式,但假设你有一个。

然后,

preg_match_all($url_regexp, $source, $matches, PREG_OFFSET_CAPTURE);

查看 $matches,您将获得每个 URL 及其位置。迭代这些以找到最接近您的子字符串位置的值。确保考虑到匹配项和子字符串的长度。

Good luck finding a regexp to match URLs, but suppose you have one.

Then,

preg_match_all($url_regexp, $source, $matches, PREG_OFFSET_CAPTURE);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文