public static function extract($regex, $str, $index = 0)
{
if (!preg_match($regex, $str, $match)) {
return false;
}
return isset($match[$index]) ? $match[$index] : null;
}
/** * Tests the `String::extract()` regex helper method. */ public function testStringExtraction() { $result = String::extract('/string/', 'whole string'); $this->assertEqual('string', $result); $this->assertFalse(String::extract('/not/', 'whole string')); $this->assertEqual('part', String::extract('/\\w+\\s*(\\w+)/', 'second part', 1)); $this->assertNull(String::extract('/\\w+\\s*(\\w+)/', 'second part', 2)); }