在PHP中使用字符串标题搜索数据

时间:2011-12-22 18:50:58

标签: php mysql strpos

我试图让脚本搜索$ open_email_msg,不同的电子邮件会有不同的信息,但格式如下。我只想要他们在标题之后放置的任何内容:或标签:或类别:无论要捕获哪些数据而不是字符串“标题:”或“标签:”或“类别:”这只是整个代码的片段,其余的将信息插入MySQL表,然后发布到网站上的新闻文章。

有人可以帮我找到解决方案吗?

严格的电子邮件格式:

  

新闻动态
  标题:文章标题
  标签:tag1 tag2   
分类:文章类别,第2条类别   
片段:文章片段   信息:   文章消息。图片。更多文字,更多文字。 Lorem impsum dolor sit amet。

<?php
//These functions searches the open e-mail for the the prefix defining strings.
    //Need a function to search after the space after the strings because the subject, categories, snippet, tags and message are constant-changing.
$subject = strpos($open_email_msg, "Title:");       //Searches the open e-mail for the string "Title" 
$categories = strpos($open_email_msg, "Categories:");       //Searches the open e-mail for the string "Categories"
$snippet = strpos($open_email_msg,"Snippet");           //Searches the open e-mail for the string "Snippet"
$content = strpos($open_email_msg, "Message");  //Searches the open-email for the string "Message"
$tags = str_replace(' ',',',$subject); //DDIE
$uri =  str_replace(' ','-',$subject); //DDIE
$when = strtotime("now");   //date article was posted

?>

2 个答案:

答案 0 :(得分:1)

试试这个:

preg_match_all('/([^:\\s]+): (.+)/', $open_email_msg, $matches, PREG_SET_ORDER);

echo '<pre>' . print_r($matches, 1) . '</pre>';

答案 1 :(得分:0)

试试这个:

$lines = explode("\r\n", $open_email_msg); // Replace with whatever the line separator is
foreach($lines as $line){
    $temp = explode(": ", $line, 2);
    $msg[$temp[0]] = $temp[1];
}
// Now we have an array like
array(
    "Title" => "Article Title",
    "Tags" => "tag1 tag2",
    "Categories" => "Article Category, 2nd Article Category",
    "Snippet" => "Article Snippet",
    "Message" => "..."
)