Question

I have an HTML file which contains nothing but text. There are no styles or anything.

The text looks like:

ID     NAME     ANOTHER-ID-11-LETTERS      MAJOR

Example:

20 Paul Mark Zedd 10203040506 Software Engineering

ID and ANOTHER-ID-11-LETTER are numbers.. NAME And MAJOR are normal text and also contain spaces.

How can I strip them and make each word or each content in new-line using PHP?

Expected result:

20
Paul Mark Zedd
10203040506
Software Engineering
Was it helpful?

Solution

Looks like the first item is always a number, followed by a space, followed by a name which can be anything, followed by a number which is 11 digits folowed by some more text.

You can use regex and the above details to split the string

$test  = preg_match("/([0-9]*?)\s(.*?)([0-9]{11})\s(.*)/is", "20 Paul Mark Zedd 10203040506 Software Engineering",$matchs);
print_r($matchs)

output:

Array
(
    [0] => 20 Paul Mark Zedd 10203040506 Software Engineering
    [1] => 20
    [2] => Paul Mark Zedd 
    [3] => 10203040506
    [4] => Software Engineering
)

OTHER TIPS

Just use a

preg_match:

#([\d]*)\s([a-zA-Z\s]*)\s([\d]*)\s([a-zA-Z\s]*)#

Example output:

array (
  0 => '20 Paul Mark Zedd 10203040506 SoftwareEngineering',
  1 => '20',
  2 => 'Paul Mark Zedd',
  3 => '10203040506',
  4 => 'SoftwareEngineering',
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top