在 PHP 中搜索字符串标题后的数据

发布于 2024-12-22 16:01:47 字数 1221 浏览 0 评论 0原文

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

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

严格的电子邮件格式:

新闻更新
标题:文章标题
标签: 标签1 标签2
类别:文章类别、第二文章类别
片段:文章片段。
信息: 文章留言。图片。更多文字,更多文字。 Lorem impsum dolor sat 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

?>

I am trying to get the scripts to search the $open_email_msg which different e-mails will have different info but the same format as below. I just want whatever they put after Title: or Tags: or Categories: whatever that data is to be captured and not the string "Title:" or "Tags:" or "Categories:" This is just a snippet of the whole code, the rest inserts the info into a MySQL table and then is posted to a News Article on the site.

Can somebody help me find a solution to this please?

Strict e-mail message format:

News Update
Title: Article Title
Tags: tag1 tag2

Categories: Article Category, 2nd Article Category

Snippet: Article snippet.
Message:
Article Message. Images. More text, more text. 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

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橪书 2024-12-29 16:01:47

试试这个:

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

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

Try this:

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

echo '<pre>' . print_r($matches, 1) . '</pre>';
喜你已久 2024-12-29 16:01:47

试试这个:

$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" => "..."
)

Try this:

$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" => "..."
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文