字符串处理 - PHP怎样去除中间字符串
巴扎黑
巴扎黑 2017-04-10 16:49:03
0
6
329

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,其他的程序完全不懂............

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

巴扎黑
巴扎黑

répondre à tous(6)
刘奇
<?php

$content = "xxxxx"; //你的内容

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

echo $result;
黄舟

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

伊谢尔伦

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

Peter_Zhu

用正则

$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
)
*/
小葫芦
$result = preg_replace("/T\d{2}:\d{2}:\d{2}Z/", " ", $str);

$str 是你的文本
黄舟

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

<?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 );
}
 ?> 

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

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal