从指定的单词拆分PHP字符串

时间:2014-03-21 09:31:39

标签: php

我有这个php字符串

$mystring ="Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >"

我想从" 2014年3月21日星期五" 开始分割字符串。我如何实现这一目标?

注意 - 溢出情况可以是一般的。也就是说它也可以在3月22日星期六和星期六或者' 3月29日星期三'等

还要提一下我应该使用哪个php函数?

8 个答案:

答案 0 :(得分:2)

因为如果你只是分开" On"它会不会很好。单词(也可能存在于之前的文本中,我假设可能有所不同),我建议以下可能性:

$str = "Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >"; if (preg_match('/^(.*)(On (Mon|Tue|Wed|Thu|Fri|Sat|Sun).*)$/', $str, $matches)) { print_r($matches); }

这会为您提供如下输出,其中应包含所有必需的值。随意添加一个" i"在preg_match正则表达式中的第二个斜杠之后,不区分大小写。

Array ( [0] => Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- > [1] => Yes YEs I am answering! [2] => On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- > [3] => Fri )

答案 1 :(得分:1)

为了做到这一点,我建议使用正则表达式,dieBeiden基本上有它,虽然我会修改他的正则表达式:

^(.*)(On (Mon|Tue|Wed|Thu|Fri|Sat|Sun), \w{3} \d{2}.*)$

答案 2 :(得分:0)

$arr = explode('Yes YEs I am answering! ', $string);

它将找到该单词,删除它,并拆分该位置数组!

然后你得到了 On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >"

接下来爆炸','

$arr = explde(',',$arr); $strings = $arr[0].','.$arr[1];

答案 3 :(得分:0)

检查一下,这个例子我找到了

<?php
$whois = "Record last updated on 10-Apr-2011.Record expires on 08-Oct-2012.Record Expires on 08-Oct-2008.";
$expires = preg_split('/Expires|expires/', $whois);
array_shift($expires);
echo "<pre>";
print_r($expires);
?>

给出

Array
(
    [0] =>  on 08-Oct-2012.Record 
    [1] =>  on 08-Oct-2008.
)

你也可以这个

http://board.phpbuilder.com/showthread.php?10384775-RESOLVED-Split-String-at-first-word-match

答案 4 :(得分:0)

尝试使用explode()

$tempArr1 = explode('!' , $mystring);
$tempArr2 = explode(',' , $tempArr1);
echo $tempArr2[0].', '.$tempArr[1].', '.$tempArr[2];

答案 5 :(得分:0)

你可以使用爆炸功能

http://in2.php.net/explode

答案 6 :(得分:0)

如果字符串的初始部分总是“是我正在回答!”,您可以使用函数删除字符串中的前24个字符

$mystring2 = substr($mystring, 24);

另请查看explode() function;)

答案 7 :(得分:0)

你可以为我们提供爆炸功能(http://nl1.php.net/explode)并将其拆分为单词&#39; On&#39;但是当&#39; On&#39;也发生在字符串中,你有麻烦。

更好的想法是使用带有preg_split(http://www.php.net/manual/en/function.preg-split.php)的正则表达式,如下所示:

<?php
$mystring ="Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >";

$splitted = preg_split('/On ..., ... [0-9]{2}, [0-9]{4} at [0-9]:[0-9]{2} (AM|PM)/', $mystring);

var_dump($splitted);


?>

随意使正则表达式更复杂:)