How to Find Next String After the Needle Using Strpos()

一曲冷凌霜 提交于 2019-12-02 00:07:34

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

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

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
}
Aaron Hathaway

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!