简体   繁体   中英

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

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',
)

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
)

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