
Untitled
By: a guest on
Apr 23rd, 2012 | syntax:
None | size: 2.13 KB | hits: 11 | expires: Never
How do I get data a regular expression does't match?
$tweet = '#notes @username !high_priority [Project Name] Tweet content';
$match = preg_match_all('/(#\w*[a-zA-Z_]+\w*)|(!\w*[a-zA-Z_]+\w*)|(@\w*[a-zA-Z_]+\w*)|(\[[^\]]*\])/i',
$tweet,
$matches);
$tweet = '@username Tweet content [Project Name] #notes !high_priority';
$data = array(
'hash' => '',
'user' => '',
'priority' => '',
'project' => '',
'content' => ''
);
$parsingProjectName = false;
foreach(explode(' ', $tweet) as $piece)
{
switch(substr($piece, 0, 1))
{
case '#':
$data['hash'] = substr($piece, 1);
break;
case '@':
$data['user'] = substr($piece, 1);
break;
case '!':
$data['priority'] = substr($piece, 1);
break;
case '[':
// Check if the project name is longer than 1 word
if(strpos($piece, -1) == ']')
{
$data['project'] = substr($piece, 1, -1);
}
else
{
// There will be more to parse in the next piece(s)
$parsingProjectName = true;
$data['project'] = substr($piece, 1) . ' ';
}
break;
default:
if($parsingProjectName)
{
// Are we at the end yet?
if(strpos($piece, -1) == ']')
{
// Yes we are
$data['project'] .= substr($piece, 1, -1);
$parsingProjectName = false;
}
else
{
// Nope, there is more
$data['project'] .= substr($piece, 1) . ' ';
}
}
else
{
// We aren't in the middle of parsing the project name, and this piece doesn't start with one of the special chars, so assume it is content
$data['content'] .= $piece . ' ';
}
}
}
// There will be an extra space on the end; remove it
$data['content'] = substr($data['content'], 0, -1);
...\s*[\w_]+\s*...