2

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!

2 Answers 2

1

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

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>;

3 Comments

I got rid of strings with "old" using "if(strpos($line, $search) !== false || strpos($line, $search2) !== false)" and outputing everything that contains $search and $search2. I need script to only take away part "{ * } * / * / * * : *-" and leave all text, that is following. I believe, that the problem is only in the $pattern. I need this "{ * } * / * / * * : *-" to be said out in php language.
@GIND The first example is not what you want? See the demo
Not really... I can`t do array like that, because the information updates all the time. My code is ok, I just need one string in code, thad says this - "{ * } * / * / * * : *-", excludes it, and echos all the rest :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.