php 在 strpos() 的字符串中使用正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8609765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using regex in a string for strpos()
提问by brinard sweeting
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.
我想让脚本搜索 $open_email_msg 不同的电子邮件将具有不同的信息但格式相同,如下所示。
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.
这只是整个代码的一个片段,其余的将信息插入到 MySQL 表中,然后发布到网站上的新闻文章中。
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.
新闻更新
标题:文章标题
标签:tag1 tag2
类别:文章类别,第二篇文章类别
片段:文章片段。
消息:文章消息。图片。更多文字,更多文字。Lorem impsum dolor 坐 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
?>
回答by cambraca
Try using the PREG_OFFSET_CAPTURE
flag for preg_match
. Something like this:
尝试将PREG_OFFSET_CAPTURE
标志用于preg_match
. 像这样的东西:
preg_match('/Title: .*/', $open_email_msg, $matches, PREG_OFFSET_CAPTURE);
echo $matches[0][1];
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:
编辑。您想要的更好的解决方案(如果我理解正确的话)将是这样的:
$title = preg_match('/Title: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';
You'd then get the title into the $title
variable, and an empty string if no title was found.
然后您将标题放入$title
变量中,如果没有找到标题则为空字符串。
回答by Shraddha
You can use preg_match instead of strpos for regex
您可以使用 preg_match 而不是 strpos 用于正则表达式
preg_match (regex, $string, $matches, PREG_OFFSET_CAPTURE);
PREG_OFFSET_CAPTURE gives you the position of match.