PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to shredding array with regx pattern in unified text?

 November 20, 2022     arrays, php, preg-replace, regex     No comments   

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:

  1. The first group will consist of numbers and letters and conclude with a digit.
  2. The second group contains no digits.
  3. The third group is consistently 2 uppercase letters.
  4. 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:

https://3v4l.org/9XpA6

~(\w+\d)([^\d ]+(?: [^\d ]+)*) ?[^A-Z]+([A-Z]{2})(\d+\.\d{3})~


Answered By - mickmackusa
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing