Search code examples
phpsubstrstrpos

strpos and substr on String


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

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
    )