开发者

Find certain text within a PHP variable string

开发者 https://www.devze.com 2023-01-31 14:37 出处:网络
Basically I\'ll be having a string that looks like #schedule take out trash #in 20 and I need to be able to pull out \"take out trash\" and \"20\" and put th开发者_如何学编程em in their own variabl

Basically I'll be having a string that looks like

#schedule take out trash #in 20

and I need to be able to pull out "take out trash" and "20" and put th开发者_如何学编程em in their own variables. Possible?


Use preg_match, for example

if (preg_match('/#schedule (.+?) #in (\d+)/', $string, $matches)) {
    // found matches
    $instruction = $matches[1];
    $time = $matches[2];
}


list($task, $delay) = preg_split("/\s?#(\w+)\s?/")

should work for anything in that general format unless you need to worry about people putting # in the task name

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号