php – 在strpos()的字符串中使用正则表达式

前端之家收集整理的这篇文章主要介绍了php – 在strpos()的字符串中使用正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要获取脚本来搜索$open_email_msg哪些不同的电子邮件将有不同的信息,但格式如下.

我没有真正使用正则表达式,但我想要做的是每当我搜索搜索的字符串“标题:[数据的标题]”,“类别:[类别的数据]我问因为我不认为像

  1. strpos($open_email_msg,"Title: (*^)");

甚至会工作.

这只是整个代码的片断,其余的将信息插入到MysqL表中,然后发布到网站上的新闻文章.

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

严格电子邮件格式:

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.

  1. <?PHP
  2. //These functions searches the open e-mail for the the prefix defining strings.
  3. //Need a function to search after the space after the strings because the subject,categories,snippet,tags and message are constant-changing.
  4. $subject = strpos($open_email_msg,"Title:"); //Searches the open e-mail for the string "Title"
  5. $subject = str_replace("Title: ","",$subject);
  6. $categories = strpos($open_email_msg,"Categories:"); //Searches the open e-mail for the string "Categories"
  7. $snippet = strpos($open_email_msg,"Snippet"); //Searches the open e-mail for the string "Snippet"
  8. $content = strpos($open_email_msg,"Message"); //Searches the open-email for the string "Message"
  9. $tags = str_replace(' ',',$subject); //DDIE
  10. $uri = str_replace(' ','-',$subject); //DDIE
  11. $when = strtotime("now"); //date article was posted
  12. ?>
尝试使用PREG_OFFSET_CAPTURE标志为 preg_match.这样的事情:
  1. preg_match('/Title: .*/',$open_email_msg,$matches,PREG_OFFSET_CAPTURE);
  2. echo $matches[0][1];

这应该给你字符串的初始位置.

请注意,我正在使用的正则表达式可能是错误的,不考虑行尾和东西,但这是另一个主题.

猜你在找的PHP相关文章