Charles Zink
Charles Zink

Reputation: 3642

Find certain text within a PHP variable string

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 them in their own variables. Possible?

Upvotes: 1

Views: 774

Answers (2)

Brad Mace
Brad Mace

Reputation: 27916

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

Upvotes: 1

Phil
Phil

Reputation: 165059

Use preg_match, for example

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

Upvotes: 2

Related Questions