PHP:过滤字符串中的特定模式
My raw output of socket_recvfrom is:
ID IP PING IDENTIFIERNUMBER USERNAME
0 127.0.0.1:1234 0 ID123456789 Moritz
1 127.0.0.1:1234 46 ID123456789 August Jones
2 127.0.0.1:1234 46 ID123456789 Miller
It is a single string that contains all of this informations in once and just contains whitespaces between the informations. All keys can be longer or shorter.
My problem:
When I preg_split("/\s+/")
it, then I get a good array with useable data, but when the username contains spaces it creates a second index for this. Not good, all data that comes after this just get destroyed.
I sort the array like this: ID, USERNAME, PING, IDENTIFIERNUMBER, IP
Example by the sorting output with username with one space in it:
ID: 0, USERNAME: Moritz, PING: 0, IDENTIFIERNUMBER: ID123456789, IP: 127.0.0.1:1234
ID: 1, USERNAME: August, PING: Jones, IDENTIFIERNUMBER: 46, IP: ID123456789
ID: 127.0.0.1:1234, USERNAME: 2, PING: Miller, IDENTIFIERNUMBER: 46, IP: ID123456789
How do I get the information correctly out of the string?
Just forgot to say:
The string begins with: ---------------------------------
in a not countable order. So it can be like 10 characters or 12.
The string ends with:
(8 users in total)
The regex methode looks good. I only need to filter out the other characters.
--------------------------------- 0 127.0.0.1:1234 0 ID123456789(OK) Moritz 1 127.0.0.1:1234 46 ID123456789(OK) August Jones 2 127.0.0.1:1234 46 ID123456789(OK) Miller (7 users in total)
Last problem: https://www.regex101.com/r/wP8cW1/1
我的socket_recvfrom的原始输出是: p>
ID IP PING IDENTIFIERNUMBER USERNAME
0 127.0.0.1:1234 0 ID123456789 Moritz p>
1 127.0.0.1:1234 46 ID123456789 August Jones p >
2 127.0.0.1:1234 46 ID123456789米勒 p>
这是一个包含所有这些信息的单个字符串 包含信息之间的空格。 所有键都可以更长或更短。 p>
我的问题: p>
当我 我按如下方式对数组进行排序:ID,USERNAME,PING,IDENTIFIERNUMBER,IP p>
使用带有一个空格的用户名的排序输出示例: p>
ID:0,USERNAME:Moritz,PING:0,IDENTIFIERNUMBER:ID123456789,IP: 127.0.0.1:1234 ID:1,USERNAME:August,PING:Jones,IDENTIFIERNUMBER:46,IP:ID123456789 p>
ID:127.0。 0.1:1234,USERNAME:2,PING:Miller,IDENTIFIERNUMBER:46,IP:ID123456789 p>
如何正确地从字符串中获取信息?
忘了说: p>
字符串以: 正则表达式方法看起来不错。 我只需要过滤掉其他字符。 p>
上一个问题:
https ://www.regex101.com/r/wP8cW1/1 p>
div> preg_split(“/ \ s + /”) code >它,然后我得到一个有可用数据的好数组,但当用户名包含空格时,它会为此创建第二个索引。 不好,这之后的所有数据都被破坏了。 p>
开头---------------- ----------------- code>以不可数的顺序。 所以它可以是10个字符或12个。
字符串以: p>
(总共8个用户)
code> pre>
-------------------------- ------- 0 127.0.0.1:1234 0 ID123456789(OK)Moritz 1 127.0.0.1:1234 46 ID123456789(OK)August Jones 2 127.0.0.1:1234 46 ID123456789(OK)Miller(共7位用户)
code> pre>
You may use regex
(?P<ID>\d+)\s+(?P<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)\s(?P<PINGR>\d+)\s(?P<IDENTIFIERNUMBER>ID\d+)(\(OK\))?(?P<USERNAME>(\s[A-z]\w+)+)
MATCH 1
ID [0-1] `0`
IP [2-16] `127.0.0.1:1234`
PINGR [17-18] `0`
IDENTIFIERNUMBER [19-30] `ID123456789`
USERNAME [31-37] `Moritz`
MATCH 2
ID [39-40] `1`
IP [41-55] `127.0.0.1:1234`
PINGR [56-58] `46`
IDENTIFIERNUMBER [59-70] `ID123456789`
USERNAME [71-83] `August Jones`
MATCH 3
ID [85-86] `2`
IP [87-101] `127.0.0.1:1234`
PINGR [102-104] `46`
IDENTIFIERNUMBER [105-116] `ID123456789`
USERNAME [117-123] `Miller`
Demo and explanation
Do you alredy try explode the string by new lines ??
test this code.
$str = '0 127.0.0.1:1234 0 ID123456789 Moritz
1 127.0.0.1:1234 46 ID123456789 August Jones
2 127.0.0.1:1234 46 ID123456789 Miller';
$lines = array_filter(explode("
", $str));
foreach ($lines as $value) {
$t[] = preg_split("/\s+/", trim($value));
}
Now in the var $t you have a usefull data.