如何使用 Strpos() 查找针后的下一个字符串
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者“旧”方式......:-)
Or the "old" way... :-)
您可以使用单个正则表达式来完成此操作:
You could do this with a single regular expression:
我使用以下内容在我的网站上做了一些测试:
我正在使用积极的后视来获得你想要的东西。
I did a little testing on my site using the following:
I'm using positive lookbehind to get what you want.