Php 如何使用Strpos()查找指针后的下一个字符串
我正在使用PHPPhp 如何使用Strpos()查找指针后的下一个字符串,php,strpos,Php,Strpos,我正在使用PHPstrpos()在一段文本中找到一根针。我正在苦苦思索如何在找到针后找到下一个单词 例如,考虑下面的段落。 $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,“SCREENSH
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”)
来检测屏幕截图是否存在,但我想在屏幕截图后获得链接,即mysite.com/SCREENSHOT.jpg
。以类似的方式,我想检测描述是否包含链接,然后返回mysite.com/LINK.html
如何使用strpos()
然后返回以下单词?我想这可能是用正则表达式完成的,但我不确定。下一个单词是“针后的空格,后跟任何东西,后跟空格”
谢谢 您可以使用单个正则表达式执行此操作:
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
}
我使用以下工具在我的网站上进行了一些测试:
$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);
$description=“您好,这是一个测试段落。我想返回的是屏幕截图mysite.com/SCREENSHOT.jpg和链接mysite.com/LINK.html。”;
$matches=array();
预匹配('/(?或“旧”方式…:-)
亲爱的,谢谢你的回复!这两项工作,我可以选择使用哪一项。:)字符串函数和正则表达式不是我的强项!
$word = "SCREENSHOT ";
$pos = strpos($description, $word);
if($pos!==false){
$link = substr($description, $pos+strlen($word));
$link = substr($link, strpos($link, " "));
}