如何使用 Strpos() 查找针后的下一个字符串

发布于 2024-10-15 19:29:35 字数 589 浏览 7 评论 0原文

我正在使用 PHP strpos() 在文本段落中查找针。我正在努力解决如何在找到针后找到下一个单词。

例如,请考虑以下段落。

$description = "Hello, this is a test paragraph.  The SCREENSHOT mysite.com/screenshot.jpg and the LINK mysite.com/link.html is what I want to return.";

我可以使用 strpos($description, "SCREENSHOT") 来检测 SCREENSHOT 是否存在,但我想获取 SCREENSHOT 之后的链接,即 mysite.com/screenshot.jpg 。以类似的方式,我想检测描述是否包含 LINK,然后返回 mysite.com/link.html

如何使用 strpos() 然后返回以下单词?我假设这可以通过正则表达式完成,但我不确定。下一个单词将是“针后一个空格,后跟任何内容,后跟一个空格”。

谢谢!

I'm using PHP strpos() to find a needle in a paragraph of text. I'm struggling with how to find the next word after the needle is found.

For example, consider the following paragraph.

$description = "Hello, this is a test paragraph.  The SCREENSHOT mysite.com/screenshot.jpg and the LINK mysite.com/link.html is what I want to return.";

I can use strpos($description, "SCREENSHOT") to detect if SCREENSHOT exists, but I want to get the link after SCREENSHOT, namely mysite.com/screenshot.jpg. In a similar fashion, I want to detect if the description contains LINK and then return mysite.com/link.html.

How can I use strpos() and then return the following word? I'm assuming this might be done with a RegEx, but I'm not sure. The next word would be "a space after the needle, followed by anything, followed by a space".

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

话少情深 2024-10-22 19:29:35

或者“旧”方式......:-)

$word = "SCREENSHOT ";
$pos = strpos($description, $word);
if($pos!==false){
    $link = substr($description, $pos+strlen($word));
    $link = substr($link, strpos($link, " "));
}

Or the "old" way... :-)

$word = "SCREENSHOT ";
$pos = strpos($description, $word);
if($pos!==false){
    $link = substr($description, $pos+strlen($word));
    $link = substr($link, strpos($link, " "));
}
別甾虛僞 2024-10-22 19:29:35

您可以使用单个正则表达式来完成此操作:

if (preg_match_all('/(SCREENSHOT|LINK) (\S+?)/', $description, $matches)) {
    $needles = $matches[1]; // The words SCREENSHOT and LINK, if you need them
    $links = $matches[2]; // Contains the screenshot and/or link URLs
}

You could do this with a single regular expression:

if (preg_match_all('/(SCREENSHOT|LINK) (\S+?)/', $description, $matches)) {
    $needles = $matches[1]; // The words SCREENSHOT and LINK, if you need them
    $links = $matches[2]; // Contains the screenshot and/or link URLs
}
南街九尾狐 2024-10-22 19:29:35

我使用以下内容在我的网站上做了一些测试:

$description = "Hello, this is a test paragraph. The SCREENSHOT mysite.com/screenshot.jpg and the LINK mysite.com/link.html is what I want to return.";

$matches = array();
preg_match('/(?<=SCREENSHOT\s)[^\s]*/', $description, $matches);
var_dump($matches);
echo '<br />';
preg_match('/(?<=LINK\s)[^\s]*/', $description, $matches);
var_dump($matches);

我正在使用积极的后视来获得你想要的东西。

I did a little testing on my site using the following:

$description = "Hello, this is a test paragraph. The SCREENSHOT mysite.com/screenshot.jpg and the LINK mysite.com/link.html is what I want to return.";

$matches = array();
preg_match('/(?<=SCREENSHOT\s)[^\s]*/', $description, $matches);
var_dump($matches);
echo '<br />';
preg_match('/(?<=LINK\s)[^\s]*/', $description, $matches);
var_dump($matches);

I'm using positive lookbehind to get what you want.

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