$text = 'cod html'; preg_match_all('/]*\btype="text"[^>]*\bname="([^"]+)"[^>]*|[^>]*\bname="([^"]+)"[^>]*\btype="text"[^>]*)>/i', $text, $matches); $subregex = array(1, 2); $names = array(); foreach($subregex as $regex) { foreach($matches[$regex] as $match) { if (trim($match)) { $names[] = trim($match); } } } print_r($names);