GIND
GIND

Reputation: 23

Multiple preg_replace rules

I have 5 string text (in .txt file):

{BANANAS} 2015/02/03 16:10 - Old look - Old Design
{BALOONS} 2015/02/03 16:23 - New look - New Design
{ORANGES} 2015/02/03 16:30 - Old look - Old Design
{BALOONS} 2015/02/03 16:50 - New look - New Design
{CARS} 2015/02/03 16:55 - New link - New Creation

I need php script, that outputs only:

New look - New Design
New look - New Design
New link - New Creation

I have this script - but it has 2 problems:

1) it works only for one type of string - with text {BALOONS}; 2) it outputs only "New Design", but I need "New look - New Design".

<?php
$search = 'New look';
$search2 = 'New link';
$lines = file('myfile.txt');


$found = false;
foreach($lines as $line)
{
  if(strpos($line, $search) !== false || strpos($line, $search2) !== false)
  {
$found = true;
echo preg_replace('/{BALOONS} (.*) - /','',$line) ."<br><br>";
  }
}


if(!$found)
{
  echo 'No match found';
}
?>

Thanks!

Upvotes: 2

Views: 217

Answers (2)

fusion3k
fusion3k

Reputation: 11689

To match every line with schema in the example and return only desired strings, you can use only one pattern:

$pattern = '
~
    ^           # start of line
    {\w+}       # any word character wrapped by curly brackets
    \s\S+       # space followed by one or more Not-spaces
    \s\S+       # space followed by one or more Not-spaces
    \s-\s       # space-dash-space
    (           # --First match group:
    New\s\w+    #   ‘New’ followed by space and one or more word chars
    \s-\s       #   space-dash-space
    New\s\w+    #   ‘New’ followed by space and one or more word chars
    )           # --END First match group
    \s*         # one-or-more spaces (you can remove this, I think)
    $           # end of line
~mx
';

/* Usage: */
preg_match_all( $pattern, $string, $matches );
echo implode( PHP_EOL, $matches[1] ) . PHP_EOL;

Will print:

New look - New Design
New look - New Design
New link - New Creation

regex101 demo

Instead, if you want retrieve any occurrence of ‘New something’, independently of your position in the line, you can use this:

$pattern = '
~
    (              # --First match group:
     (             #   --Second match group:
      (New\s\w+)   #     --3rd match group: ‘New’ followed by space and one-or-more word chars
      (\s-\s)?     #     --4st match group: optional space-dash-space
     )+            #   --END Second match group (repeating one-or-more)
    )              # --END First match group
~mx
';

/* Usage: */
preg_match_all( $pattern, $string, $matches );
echo implode( PHP_EOL, $matches[0] ) . PHP_EOL;

Probably you want search for a variable, not for a fixed string. For this, simply replace New with your {$variable}, like in this example:

$find = 'Old';
$pattern = "~^{\w+}\s\S+\s\S+\s-\s({$find}\s\w+\s-\s{$find}\s\w+)\s*$~m";
//                                 -------          -------

(The pattern above is the same of first example, in one line).

Edit:

To apply above pattern to each line inside your original foreach, simply remove first match group and your preg_replace will work:

$pattern = '~^{\w+}\s\S+\s\S+\s-\s+~';  // no multiline option needed
echo preg_replace( $pattern, '', $line ) . '<br><br>;

Upvotes: 0

Halayem Anis
Halayem Anis

Reputation: 7785

You pattern is incorrect, you can try as follow

$src        = '{BANANAS} 2015/02/03 16:10 - Old look - Old Design
              {BALOONS} 2015/02/03 16:23 - New look - New Design
              {ORANGES} 2015/02/03 16:30 - Old look - Old Design
              {BALOONS} 2015/02/03 16:50 - New look - New Design
              {CARS} 2015/02/03 16:55 - New link - New Creation';
$pattern    = '#\{[A-Z]+\} [0-9]{4,4}/[0-9]{2,2}/[0-9]{2,2} [0-9]{2,2}:[0-9]{2,2} - (New [A-Za-z]+ - New [A-Za-z]+)#';

preg_match_all($pattern, $src, $matches);
print_r ($matches);

OUTPUT

Array
(
    [0] => Array
        (
            [0] => {BALOONS} 2015/02/03 16:23 - New look - New Design
            [1] => {BALOONS} 2015/02/03 16:50 - New look - New Design
            [2] => {CARS} 2015/02/03 16:55 - New link - New Creation
        )

    [1] => Array
        (
            [0] => New look - New Design
            [1] => New look - New Design
            [2] => New link - New Creation
        )

)

Upvotes: 1

Related Questions