how do i extract data from string (php) using preg_match?
Get the solution ↓↓↓I have a string,
DXD RO 012345 SMITH JOHN 094 5251 907 REQ 008313 M
I am trying to extract the phone number, which might have 5-15 digits, with unknown amount of white spaces. Also i need the last letter (sex) and the first name.
what is the exact pattren to do so ?
Answer
Solution:
$subject = "DXD RO 012345 SMITH JOHN 094 5251 907 REQ 008313 M";
$pattern = "/[a-z]+\s+[a-z]+\s+\d+\s+[^\d]+\s*(\d+\s*\d+\s*\d+)\s+[a-z\-]+\s+\d+\s+([a-z])/i";
preg_match($pattern, $subject, $matches);
$matches[1] contains the telephone number and $matches[2] contains the sex.
For this, I assumed that columns 1,2,4,5 and 9 only contain letters, and 3,6,7,8,10 only digits(the sex has to be a letter).
However, if you need to have digits as well as letters, just replace [a-z] or \d with [a-z0-9] for the respective column(\s is whitespace)
Answer
Solution:
For phone You can try:
$subject = "DXD RO 012345 SMITH JOHN 094 5251 907 REQ 008313 M";
preg_match_all("/([0-9]|\s).+{5,15}(\s)/si", $subjectt, $matches);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illuminate\http\exceptions\posttoolargeexception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.