2

I have a script that is watching the last line of a log file in real time. When the following line appears. I want to extract the data inside the second set of brackets. My original code worked fine, problem is that the log output has changed. Sample log output:

2016-04-28 16:54:49     INFO    [ADMINCGI sid=1] Title updated [Peezy - Dope Fein Baby (Feat. TID Sweeze & H.N.I.C Pesh)]
2016-04-28 16:54:49     INFO    [ADMINCGI sid=1] DJ name updated [1]

I need to first ensure that the line in question line #1 is the line found and then extract the text inside second brackets if possible.

I am using this but it is returning the #2 line

My code:

$line ="2016-04-27 22:56:48     INFO    [ADMINCGI sid=1] Title updated [Tessa Feat. Gucci Mane - Get You Right]";
echo $line;

$pattern="/\[([^\]]*)\]/";

$needle = " Title updated ";
if (strpos($line,$needle) !== false) {
preg_match_all($pattern, $line, $matches);
foreach ($matches[1] as $a ){

  //  echo $a."</br>";
  //  echo $matches[1][1];
    $fulltitle = explode("-", $matches[1][1]);

    $artist = $fulltitle[0];
    $title = $fulltitle[1];
    echo $artist;
5
  • Why do you iterate an array if you access the entry directly ? Maybe I bother tooo much for details, by the way many online regexp tester could help you test your pattern. regexr.com for exemple. Commented Apr 28, 2016 at 21:09
  • 1
    If you test each line individually, try using preg_match_all('~\[([^][]*)]~', $input, $m); $my_val = $m[1][1]; Commented Apr 28, 2016 at 21:14
  • @BuzzzHost.com: Any feedback? Does any suggestion work for you? Commented Apr 28, 2016 at 21:35
  • I need this inside an if statement to mean "If this line is found then do this or don't this" thanks a lot for your assistance. After Artist and Title is extracted, it is sent to another script that posts to Twitter. I am running this code using watch -n 1 php script.php and it continuously executes. I need it to where it can continuously run to monitor the last line and trigger only if that title updated line is found and then grab the artist and title. Sorry for not being more direct. Commented Apr 29, 2016 at 0:16
  • 2016-04-28 21:39:12 INFO [ADMINCGI sid=1] Title updated [Kevin Gates - Making Love (DatPiff Exclusive)] 2016-04-28 21:39:12 INFO [ADMINCGI sid=1] DJ name updated [1] Right after the song changes, the 2nd line above is outputed.The regex is trying to pull data from that line due to the [1] i believe. I need a way to specifically take the Artist ad Song Title only and ignore everything else. I am not the best with this stuff so I apologize but I need code to say, find anything within [] delimited by a hypen. That is the only difference between to the two lines that I can think of. Commented Apr 29, 2016 at 1:49

1 Answer 1

1

I would go with using the strrpos() function because the main line is much simpler to debug than that regex

<?php
$line ="2016-04-27 22:56:48     INFO    [ADMINCGI sid=1] Title updated [Tessa Feat. Gucci Mane - Get You Right]";
echo $line;

$match = rtrim( substr( $line, strrpos($line, '[') + 1), "]");

$fulltitle = explode("-", $match);

$artist = $fulltitle[0];
$title = $fulltitle[1];
echo $artist;

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

4 Comments

Thanks for the responses :)
Last thing and thank you for the feedback. This works and was able to extract the "Artist" and "Song Title" without issue. Problem is that the script is running continuously and I need it to only fire when the song changes. Every time the song changes, that line will appear in the logs except with a different artist and title. I had it where every time the song changes, it creates a tweet and sends it to Twitter. This works but what happens is that, it keeps tweeting non stop. It should only do this once per song change.
2016-04-28 21:39:12 INFO [ADMINCGI sid=1] Title updated [Kevin Gates - Making Love (DatPiff Exclusive)] 2016-04-28 21:39:12 INFO [ADMINCGI sid=1] DJ name updated [1] Right after the song changes, the 2nd line above is outputed.The regex is trying to pull data from that line due to the [1] i believe. I need a way to specifically take the Artist ad Song Title only and ignore everything else. I am not the best with this stuff so I apologize but I need code to say, find anything within [] delimited by a hypen. That is the only difference between to the two lines that I can think of.
This second issue is better suited in a separate question. Eventually post the link to the new question in a comment to this one. If my answer to this problem solved your issue, please upvote & accept it.

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.