简体   繁体   中英

preg_split array

On running:

preg_split("/MapAttempt /", $subject)

on the given input string:

MapAttempt TASK_TYPE="CLEANUP" TASKID="task_201307250256_0001_m_000320" TASK_ATTEMPT_ID="attempt_201307250256_0001_m_000320_0" START_TIME="1374702854132" TRACKER_NAME="tracker_wsmex3:ip6-localhost/127\.0\.0\.1:43273" HTTP_PORT="50060" .
MapAttempt TASK_TYPE="CLEANUP" TASKID="task_201307250256_0001_m_000320" TASK_ATTEMPT_ID="attempt_201307250256_0001_m_000320_0" TASK_STATUS="SUCCESS" FINISH_TIME="1374702864491" HOSTNAME="/default-rack/wsmex3" STATE_STRING="cleanup" COUNTERS="{(FileSystemCounters)(FileSystemCounters)[(FILE_BYTES_WRITTEN)(FILE_BYTES_WRITTEN)(21559)]}{(org\.apache\.hadoop\.mapred\.Task$Counter)(Map-Reduce Framework)[(PHYSICAL_MEMORY_BYTES)(Physical memory \\(bytes\\) snapshot)(95113216)][(SPILLED_RECORDS)(Spilled Records)(0)][(CPU_MILLISECONDS)(CPU time spent \\(ms\\))(690)][(COMMITTED_HEAP_BYTES)(Total committed heap usage \\(bytes\\))(200998912)][(VIRTUAL_MEMORY_BYTES)(Virtual memory \\(bytes\\) snapshot)(1214373888)]}

I'm receiving the required arrays but also an additional empty index, why?

And also why is that running:

preg_split("/MapAttempt .* \./", $subject)

returns empty arrays?

You have 2 questions.

The reason you get an additional empty index is because there is an empty string in front of the first MapAttempt.

preg_split('/A/', 'xAyAz');

would result in

array('x', 'y', 'z')

whereas

preg_split('/A/', 'AyAz');

results in an

array ('', 'y', 'z')

just like in the above example it gives you everything that is before the first delineator in the first element of the array - but there's nothing there, so it gives you an empty string.

For your second question, you are asking for to divide the $subject up by the following:

The string "MapAttempt" followed by a space followed by 0 or more of anything followed by a space followed by a period.

The 2nd question is a slightly more complicated example of the first. Your regular expression is designed to match the entire first line in the subject. So you are splitting based on what appears 1st in the $subject. Therefore you get what comes before the delineator (empty string since the delineator matches at the very beginning), then none of the first line prints because it is all encompassed in the delineator (just as preg_split('/A/', ...) doesn't give you the "A" in the returned array) and then it gives you everything after the delineator (the entire 2nd line).

(If this solves your problem, please mark as the answer. I'm trying to get enough reputation points so I can vote on something! Thanks...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM