在 strpos() 的字符串中使用正则表达式
我想让脚本搜索 $open_email_msg ,不同的电子邮件将具有不同的信息,但格式相同,如下所示。
我并没有真正使用正则表达式太多,但我想做的是每当我用它来搜索字符串时,它都会搜索“标题:[标题数据]”,“类别:[类别数据]。我问因为我认为类似的东西
strpos($open_email_msg, "Title: (*^)");
根本行不通。
这只是整个代码的一个片段,其余的将信息插入到 MySQL 表中,然后发布到网站上的新闻文章中,
有人可以帮我找到解决方案吗? 吗?
严格的电子邮件 消息格式:
<块引用>新闻更新
标题:文章标题
标签:tag1 tag2
类别:文章类别、第二文章类别
片段: 文章片段。
消息:文章消息。图片。更多文字, 更多文字。 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"
$subject = str_replace("Title: ", "" ,$subject);
$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 want 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 haven't really used regex much, but what i want to do is whenever i have it to search the string it would search for "Title: [data for title]", "Categories: [data for categories]. I am asking because i don't think something like
strpos($open_email_msg, "Title: (*^)");
would even work.
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"
$subject = str_replace("Title: ", "" ,$subject);
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
preg_match
PREG_OFFSET_CAPTURE 标志代码>。像这样的东西:这应该给你字符串的初始位置。
请注意,我使用的正则表达式可能是错误的,并且没有考虑行结尾和其他内容,但这是另一个主题。 :)
编辑。对于您想要的更好的解决方案(如果我理解正确的话)是这样的:
然后您将标题放入
$title
变量中,如果没有找到标题,则将其放入空字符串。Try using the
PREG_OFFSET_CAPTURE
flag forpreg_match
. Something like this:This should give you the initial position of the string.
Note that the regex I'm using could be wrong and not take into account line endings and stuff, but that's another subject. :)
EDIT. A better solution for what you want (if I understand it correctly) would be something like this:
You'd then get the title into the
$title
variable, and an empty string if no title was found.您可以使用 preg_match 而不是 strpos 进行正则表达式
You can use preg_match instead of strpos for regex