gpt4 book ai didi

php - PHP-字符串修剪

转载 作者:行者123 更新时间:2023-12-04 19:18:26 25 4
gpt4 key购买 nike

请帮我。

$str = "col_1          col_2  col_3
row1 row2 row3
row12 row22 row33";

$arr = explode("\n", $str);

foreach($arr as $line)
{
$temp_arr = explode(WHAT HERE, $line);
}


我如何将每一行爆炸成数组?
我要 $temp_arr[0] = col_1$temp_arr[1] = col2$temp_arr[2] = col_3
下一行将具有 $temp_arr[0] = row1$temp_arr[1] = row2

你们真棒。它适用于我的示例,但不适用于我的实际问题。我导出Windows XP进程,并希望将其爆炸。

字幕命令行ProcessId
系统空闲进程0
系统4
smss.exe \ SystemRoot \ System32 \ smss.exe 604
avgrsx.exe C:\ PROGRA〜1 \ AVG \ AVG10 \ avgrsx.exe /重新启动/启动692
csrss.exe C:\ WINDOWS \ system32 \ csrss.exe ObjectDirectory = \ Windows SharedSection = 1024,3072,512 Windows =在SubSystemType = Windows ServerDll = basesrv,1 ServerDll = winsrv:UserServerDllInitialization,3 ServerDll = winsrv:ConServerDllInitialization,2 ProfileControl =关闭MaxRequestThreads = 16780
winlogon.exe winlogon.exe 820
services.exe C:\ WINDOWS \ system32 \ services.exe 880
lsass.exe C:\ WINDOWS \ system32 \ lsass.exe 892
nvsvc32.exe C:\ WINDOWS \ system32 \ nvsvc32.exe 1052
svchost.exe C:\ WINDOWS \ system32 \ svchost -k DcomLaunch 1168
svchost.exe C:\ WINDOWS \ system32 \ svchost -k rpcss 1240
svchost.exe C:\ WINDOWS \ System32 \ svchost.exe -k netsvcs 1328
svchost.exe C:\ WINDOWS \ system32 \ svchost.exe -k NetworkService 1404
svchost.exe C:\ WINDOWS \ system32 \ svchost.exe -k LocalService 1520
spoolsv.exe C:\ WINDOWS \ system32 \ spoolsv.exe 1664
C:\ WINDOWS \ Explorer.EXE 1956年
jqs.exe“ C:\ Program Files \ Java \ jre6 \ bin \ jqs.exe” -service -config“ C:\ Program Files \ Java \ jre6 \ lib \ deploy \ jqs \ jqs.conf” 460
avgtray.exe“ C:\ Program Files \ AVG \ AVG10 \ avgtray.exe” 1156
rundll32.exe“ C:\ WINDOWS \ system32 \ RUNDLL32.EXE” C:\ WINDOWS \ system32 \ NvMcTray.dll,NvTaskbarInit 1204
alg.exe C:\ WINDOWS \ System32 \ alg.exe 1376
AVGIDSMonitor.exe“ C:\ Program Files \ AVG \ AVG10 \ Identity Protection \ agent \ bin \ avgidsmonitor.exe” 660
iexplore.exe“ C:\ Program Files \ Internet Explorer \ iexplore.exe” 620
OUTLOOK.EXE“ C:\ Program Files \ Microsoft Office \ Office12 \ OUTLOOK.EXE” /回收1884
SQLyog.exe“ C:\ Program Files \ SQLyog \ SQLyog.exe” 2596
UniKeyNT.exe“ C:\ Program Files \ unikey40RC2-1101-win32 \ UniKeyNT.exe” 964
avgcsrvx.exe C:\ Program Files \ AVG \ AVG10 \ avgcsrvx.exe / pipeName = 4f758821-d931-4842-ae08-b81fe89b843a / coreSdkOptions = 0 / binaryPath =“ C:\ Program Files \ AVG \ AVG10 \” / registryPath = “ SYSTEM \ CurrentControlSet \ Services \ Avg \ Avg10” 3788
svchost.exe C:\ WINDOWS \ system32 \ svchost.exe -k imgsvc 3604
mstsc.exe“ C:\ WINDOWS \ system32 \ mstsc.exe” 3868
notepad ++。exe“ C:\ Program Files \ Notepad ++ \ notepad ++。exe” 4072
notepad.exe“ C:\ WINDOWS \ system32 \ NOTEPAD.EXE” C:\ Documents and Settings \ Quy Nguyen \ Desktop \ cool.txt 2228
cmd.exe“ C:\ WINDOWS \ system32 \ cmd.exe” 2552
wmic.exe WMIC /OUTPUT:C:\ProcessList.txt过程获取标题,命令行,Processid 2480
wmiprvse.exe C:\ WINDOWS \ system32 \ wbem \ wmiprvse.exe 1744

最佳答案

更换

$temp_arr = explode(WHAT HERE, $line);




$temp_arr = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);


编辑:添加了PREG_SPLIT_NO_EMPTY

好,所以您的问题有所改变。该代码应完全满足您的要求:

$processes = 'the list you posted';

$temp = preg_split('/\s*(?:\r?\n)+/', $processes, -1, PREG_SPLIT_NO_EMPTY);
$process_list = array();
foreach ($temp as $process) {
$process_list[] = array_pad( // array_pad makes the length consistent
preg_split(
'/\s{2,}/', // match only 2 or more consecutive spaces for splitting
$process,
3,
PREG_SPLIT_NO_EMPTY), // ignore empty matches (i.e., end of line)
3, // pad array to a length of 3
''
);
}

$process_list_final = array();
$process_list_titles = array_shift($process_list);

foreach ($process_list as &$plist_item) {
$plist_item = array_combine($process_list_titles, $plist_item);
}


它可能需要一些清理,但是您可以自己完成=)

一些功能供您查找:


preg_split
array_pad
array_shift
array_combine

关于php - PHP-字符串修剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6513130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com