首页 >后端开发 >php教程 >字符串处理 - PHP怎样去除中间字符串

字符串处理 - PHP怎样去除中间字符串

WBOY
WBOY原创
2016-06-06 20:18:081577浏览

6691.club,已经注册,2015-06-17T17:15:08Z,2016-06-16
6688.club,已经注册,2014-05-07T15:02:16Z,2016-05-06
6689.club,已经注册,2015-06-10T06:03:05Z,2016-06-09
6695.club,已经注册,2015-06-10T14:32:14Z,2016-06-09
6694.club,已经注册,2015-11-08T15:01:24Z,2016-11-07
6693.club,已经注册,2015-06-17T17:20:25Z,2016-06-16
6692.club,已经注册,2015-06-17T17:14:43Z,2016-06-16
6690.club,已经注册,2015-06-22T08:43:14Z,2016-06-21
6605.club,已经注册,2015-06-25T08:43:19Z,2016-06-24
6606.club,已经注册,2015-05-07T15:56:01Z,2016-05-06
6603.club,已经注册,2015-06-25T08:43:17Z,2016-06-24
6604.club,已经注册,2015-11-09T09:13:15Z,2016-11-08
6681.club,已经注册,2015-06-17T13:44:15Z,2016-06-16
6608.club,已经注册,2015-06-10T09:27:52Z,2016-06-09
6607.club,已经注册,2015-06-25T08:43:21Z,2016-06-24
6682.club,已经注册,2015-06-17T15:43:28Z,2016-06-16

这样的,我希望去除T15:43:28Z这段,规律是T开头,Z结尾.

我机器上只有PHP环境,看得点东PHP,其他的程序完全不懂............

求高手指教。。。。。。。

回复内容:

6691.club,已经注册,2015-06-17T17:15:08Z,2016-06-16
6688.club,已经注册,2014-05-07T15:02:16Z,2016-05-06
6689.club,已经注册,2015-06-10T06:03:05Z,2016-06-09
6695.club,已经注册,2015-06-10T14:32:14Z,2016-06-09
6694.club,已经注册,2015-11-08T15:01:24Z,2016-11-07
6693.club,已经注册,2015-06-17T17:20:25Z,2016-06-16
6692.club,已经注册,2015-06-17T17:14:43Z,2016-06-16
6690.club,已经注册,2015-06-22T08:43:14Z,2016-06-21
6605.club,已经注册,2015-06-25T08:43:19Z,2016-06-24
6606.club,已经注册,2015-05-07T15:56:01Z,2016-05-06
6603.club,已经注册,2015-06-25T08:43:17Z,2016-06-24
6604.club,已经注册,2015-11-09T09:13:15Z,2016-11-08
6681.club,已经注册,2015-06-17T13:44:15Z,2016-06-16
6608.club,已经注册,2015-06-10T09:27:52Z,2016-06-09
6607.club,已经注册,2015-06-25T08:43:21Z,2016-06-24
6682.club,已经注册,2015-06-17T15:43:28Z,2016-06-16

这样的,我希望去除T15:43:28Z这段,规律是T开头,Z结尾.

我机器上只有PHP环境,看得点东PHP,其他的程序完全不懂............

求高手指教。。。。。。。

<code><?php $content = "xxxxx"; //你的内容

$result = preg_replace("/T.*Z/i","",$content);

echo $result;
</code></code>

用PHP处理小文件可以,文件大了还是用sed比较快
sed -i "s/T.*Z//g" test.txt

如果用正则匹配的话,可能注册的用户名中也有TZ字符,所以可以考虑字符串截取 毕竟T17:15:08Z,2016-06-16这一段的长度是固定的

用正则

<code>$subject = '6691.club,已经注册,2015-06-17T17:15:08Z,2016-06-16';
$pattern = '/(.*,[\d]{4}-[\d]{2}-[\d]{2})T[\d:]{8}Z(,[\d]{4}-[\d]{2}-[\d]{2}$)/';
preg_match($pattern, $subject, $matches);
print_r($matches);
/*
Array
(
[0] => 6691.club,已经注册,2015-06-17T17:15:08Z,2016-06-16
[1] => 6691.club,已经注册,2015-06-17
[2] => ,2016-06-16
)
*/</code>

<code>$result = preg_replace("/T\d{2}:\d{2}:\d{2}Z/", " ", $str);

$str 是你的文本</code>

谢谢各位,通过学习楼上诸位的代码,我自己学习加工了下:
全段代码如下:

<code><?php $handle  = @fopen ("4.txt","r" );
if ( $handle ) {
    while (( $buffer  =  fgets ($handle ,4096 )) !==  false ) {
       // echo  $buffer."<br>\n";
        echo preg_replace('/\w\d\d\:\d\d\:\d\d\w/',"",$buffer)."<br>\n";
    }
    if (! feof ( $handle )) {
        echo  "Error: unexpected fgets() fail\n" ;
    }
     fclose ( $handle );
}
 ?> 
</code>

基本上达到我要的目的了,再次感谢楼上各位的帮忙

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn