Issue
I'm converting text from a txt file into an array.I need to shred the texts in this array using regex.
This is the array in my text file.
Array
(
[0] => 65S34523APPLE IS VERY BEAUTIFUL6.000TX786.34563.675 234.89
[1] => 06W01232BOOK IS SUCCESSFUL1.000YJ160.00021.853 496.00
[2] => 67E45643DO YOU HAVE A PEN? 7/56.450EQ9000.3451.432 765.12
)
if I need to explain a line as an example,
input => 65S34523APPLE IS VERY BEAUTIFUL6.000TX786.34563.675 234.89
required sections => 65S34523 APPLE IS VERY BEAUTIFUL 6.000 TX 786.345 63.67 5 234.89
target I want :
Array
(
[0] => 65S34523
[1] => APPLE IS VERY BEAUTIFUL
[2] => TX
[3] => 786.345
)
I need multiple regex patterns to achieve this.I need to shred the data I want in order in a loop.but since there is no specific layout, I don't know what to choose according to the regex patterns.
I've tried various codes to smash this array.
$smash =
array('65S34523APPLE IS VERY BEAUTIFUL6.000TX786.34563.675 234.89',
'06W01232BOOK IS SUCCESSFUL1.000YJ160.00021.853 496.00',
'67E45643DO YOU HAVE A PEN? 7/56.450EQ9000.3451.432 765.12');
I'm trying to foreach and parse the array.For example, I tried to get the text first.
foreach ($smash as $row) {
$delete_numbers = preg_replace('/\d/', '', $smash);
}
echo "<pre>";
print_r($delete_numbers);
echo "</pre>";
While it turned out it was that way.
Array
(
[0] => SAPPLE IS VERY BEAUTIFUL.TX.. .
[1] => WBOOK IS SUCCESSFUL.YJ.. .
[2] => EDO YOU HAVE A PEN? /.EQ.. .
)
Naturally, this is not what I want.Each array has a different structure.So i have to check with if-else too.
As you can see in the example, there is no pure text.Here TX,YJ,EQ should be deleted.The dots should be wiped using apples.The first letters at the beginning of the text should be removed.The remaining special characters must be removed.
I have tried many of the above.I have looked at alternative examples.
AS A RESULT;
I'm in a dead end.
Solution
Code: (Demo)
$smash = ['65S34523APPLE IS VERY BEAUTIFUL6.000TX786.34563.675 234.89',
'06W01232BOOK IS SUCCESSFUL1.000YJ160.00021.853 496.00',
'67E45643DO YOU HAVE A PEN? 7/56.450EQ9000.3451.432 765.12'];
foreach ($smash as $line) {
$result[] = preg_match('~(\w+\d)(\D+)[^A-Z]+([A-Z]{2})(\d+\.\d{3})~', $line, $out) ? array_slice($out, 1) : [];
}
var_export($result);
Output:
array (
0 =>
array (
0 => '65S34523',
1 => 'APPLE IS VERY BEAUTIFUL',
2 => 'TX',
3 => '786.345',
),
1 =>
array (
0 => '06W01232',
1 => 'BOOK IS SUCCESSFUL',
2 => 'YJ',
3 => '160.000',
),
2 =>
array (
0 => '67E45643',
1 => 'DO YOU HAVE A PEN? ',
2 => 'EQ',
3 => '9000.345',
),
)
My pattern assumes:
- The first group will consist of numbers and letters and conclude with a digit.
- The second group contains no digits.
- The third group is consistently 2 uppercase letters.
- The fourth group will reliably have three decimal places.
p.s. If you don't want that pesky trailing space after PEN?
, you could use this:
~(\w+\d)([^\d ]+(?: [^\d ]+)*) ?[^A-Z]+([A-Z]{2})(\d+\.\d{3})~
Answered By - mickmackusa Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.