- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想让脚本搜索 $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_match
使用 PREG_OFFSET_CAPTURE
标志.像这样的:
preg_match('/Title: .*/', $open_email_msg, $matches, PREG_OFFSET_CAPTURE);
echo $matches[0][1];
这应该会为您提供字符串的初始位置。
请注意,我使用的正则表达式可能是错误的,并且没有考虑行结尾和其他内容,但这是另一个主题。 :)
编辑。你想要的更好的解决方案(如果我理解正确的话)是这样的:
$title = preg_match('/Title: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';
然后您会将标题放入 $title
变量中,如果没有找到标题,则为空字符串。
关于php - 在 strpos() 的字符串中使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8609765/
Warning: strpos() [function.strpos]: Empty delimiter 是什么意思? 我有这个: if(strpos(''', $text) ===
我知道,有人问过这个问题,但不幸的是,没有解决这个问题的答案。 这出现在我的日志文件中: PHP 消息:PHP 警告:strpos():第 479 行中的字符串中不包含偏移量 不幸的是,我无法理解导致
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章strpos() 函数判断字符串中是否包含某字符串的方法由作者收集整理,
问题,下面是怎么执行echo的: $str = "Hello World"; if (strpos($str, 'He') !== false) { echo 'GOOD'; } 但这不是:
我使用的是 PHP 版本 5.1.6 我有一个字符串( session 文件)并且需要从中提取一个值,所以我在字符串中搜索针但它返回 false,我将代码缩减为: $string = ';SERVER
我正在使用这段代码来查找字符串中是否存在子字符串,但是当它遇到特殊字符时,即使该字符明显存在,它也会失败。 //Code out of context if (strpos($pieces[$i],
所以目前我有以下两个数组: Array ( [0] => image/jpeg [1] => image/png [2] => image/psd ) Array ( [0] => png [1] =
我需要检查字符串是否包含特定单词,但请记住,中间可能有空格(任何类型 - 空格、制表符等)。 $array1 = "name @mail lastname"; $array2 = "name @ ma
因此,我的网站收到了大量垃圾邮件。 为了过滤掉其中的一些内容,我想测试帖子的正文以确保它不包含某些词。如果他们这样做,请立即(临时)禁止用户。 包括我的代码。我添加了一条回显线来显示返回的位置,并测试
我正在尝试使用 strpos 在另一个字符串中查找字符串,但由于某种原因它不适用于某个字符串,即使它适用于另一个字符串。我究竟做错了什么? 结果: true2 预期结果: true1true2 最佳
我在 php 中有一个函数,我想对一个字符串执行简单的搜索,使用 kw 作为搜索短语,如果找到则返回 true。 这是我现在拥有的: for($i=0; $i
我一直在努力反对谷歌试图解决这个问题,但我所能找到的只是那些不明白 strpos 返回找到的字符串的位置的人,所以 0 是一个有效值。这不是我的问题......简而言之: $services=shel
我有一个巨大的 html 代码要扫描。直到现在我一直在使用 preg_match_all从中提取所需的部分。从一开始的问题就是它非常耗费 CPU 时间。我们最终决定使用其他方法进行提取。我在一些文章中
我正在尝试用 PHP 进行一些验证,其中之一是检查输入的字符串中是否有特定的单词。问题是,当我将指定的单词放在第一位时,我的代码似乎无法正常工作。 代码如下: $word = "aa bb cc dd
更新:我在查看strrpos 的文档时认为它是strpos 的文档。很抱歉犯了这个愚蠢的错误。 根据 the docs ,它表示当您提供一个负值作为偏移量参数时,该函数将从字符串末尾开始那么多字符,向
我在 stackoverflow 上找到了这个例子: if (strpos($a,'are') !== false) { echo 'true'; } 但是我如何让它搜索两个词。我需要这样的东
我需要找出一个字符串是否存在于数组值中,但不一定是实际的数组值。 $array = array( 0 => 'blue', 1 => 'red', 2 =>
仅当 $_GET['page'] 参数包含文本“mytext-”时,我才需要执行一些脚本 查询字符串是:admin.php?page=mytext-option 这将返回 0: $myPage = $
我已经为我的网络应用编写了一个基本的“安全检查程序”。我需要看一眼用户提交的代码是否包含恶意内容。 这是我现在正在运行的代码的屏幕截图:http://cl.ly/677a6dc40034f096697
我正在使用 strpos() 函数在数组键成员中查找字符串。 foreach($_POST as $k => $v){ // for all types of questions of
我是一名优秀的程序员,十分优秀!