-1

我在 PHP 中有这个字符串:

Hopelessly Incredible |SPG:M| 766 STEAM_0:1:20130600 " banned "A
Blatantly Obvious Hacker 740 STEAM_0:1:55386073 " (minutes "0")
(reason "Multi-Hack")

字符串的格式为:

Name1 Number(0-3 digits) Steam_0:x:xxxx offense (banned/kicked/mute etc) Name2 Number(0-3 digits) Steam_0:x:xxxx time reason

我的目标是删除这些值766740因为它只是垃圾。这些值可以有一位数、两位数和三位数。

下一步是剥离字符串STEAM_0:1:20130600并将STEAM_0:1:55386073其捕获到一个新变量中。唯一不变的是STEAM_0:,其余的都可以改变。

我仍在学习正则表达式,但我担心这对我来说有点复杂。一些指导将是最感激的。

4

2 回答 2

0

试试这个:(不擅长 PHP,但这应该很接近,正则表达式应该可以工作)

<?php
$sourcestring='Hopelessly Incredible |SPG:M| 766 STEAM_0:1:20130600 " banned "A Blatantly Obvious Hacker 740 STEAM_0:1:55386073 " (minutes "0") (reason "Multi-Hack")';
$replacedstring = preg_replace('/\d{1,3}(?=\s*STEAM_0)/i','',$sourcestring);
echo $replacedstring;
preg_match_all('/STEAM_0[^\s]+/i',$replacedstring,$matches);
echo "<pre>".print_r($matches,true);
?>

在这里玩代码


  • 正则表达式/\d{1,3}(?=\s*STEAM_0)/i匹配之前出现的数字 STEAM_0(正前瞻)并用空字符串替换它们
  • 正则表达式/STEAM_0[^\s]+/i匹配所有STEAM_0单词(不区分大小写)并打印所有匹配的数组。
于 2012-09-12T20:41:14.453 回答
-1
$ cat scratch.php
<?php
$s = 'Hopelessly Incredible |SPG:M| 766 STEAM_0:1:20130600 " banned "A Blatantly Obvious Hacker 740 STEAM_0:1:55386073 " (minutes "0") (reason "Multi-Hack")';

$s = preg_replace('@\d+ (STEAM_\d:)@', '\1', $s);
preg_match_all('@(STEAM_\d:\d+:\d+)@', $s, $matches);
var_dump($s, $matches[1]);


$ php scratch.php
string(142) "Hopelessly Incredible |SPG:M| STEAM_0:1:20130600 " banned "A Blatantly Obvious Hacker STEAM_0:1:55386073 " (minutes "0") (reason "Multi-Hack")"
array(2) {
  [0]=>
  string(18) "STEAM_0:1:20130600"
  [1]=>
  string(18) "STEAM_0:1:55386073"
}
于 2012-09-12T20:39:09.807 回答