duanlan4801 2012-12-21 22:52
浏览 88
已采纳

PHP提取字符串的一部分

I know there's a lot of questions about this but it seems there's no simple way to do what I want. Take the following strings as an example:

expires  Friday, December 21, 2012 @ 11:59pm ET
expires  Saturday, December 22, 2012 @ 9:59pm ET

Both strings are similar but the following code is inconsistent:

echo substr($string, 14, 27);

as it outputs the following:

Friday, December 21, 2012 @
Saturday, December 21, 2012

Clearly, I don't want to keep the @ symbol. Is there a way to simply keep the date while removing "expires " and " @ ##:##pm ET"?

  • 写回答

3条回答 默认 最新

  • 普通网友 2012-12-21 22:55
    关注
    preg_replace('/expires\s*(.*?)@/', '\1', $string);
    

    You also don't really need to use a regex, but I would since it looks simpler. Here is a non-regex solution:

    //I took 14 from you, but it seems to be 9
    substr($string, 14, strpos($string, '@') - strlen($string));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?