加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 14:03:24 所属栏目:PHP教程 来源:网络整理
导读:我想要获取脚本来搜索$open_email_msg哪些不同的电子邮件将有不同的信息,但格式如下. 我没有真正使用正则表达式,但我想要做的是每当我搜索要搜索的字符串“标题:[数据的标题]”,“类别:[类别的数据]我问因为我不认为像 strpos($open_email_msg,"Title: (*^
我想要获取脚本来搜索$open_email_msg哪些不同的电子邮件将有不同的信息,但格式如下.

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

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.

<?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
?>
尝试使用PREG_OFFSET_CAPTURE标志为 preg_match.这样的事情:
preg_match('/Title: .*/',$open_email_msg,$matches,PREG_OFFSET_CAPTURE);
echo $matches[0][1];

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读