Conner
Conner

Reputation: 123

Filter lines in txt files and make them http href links

What I want to do this time is get the number between the ( and ), filter them out so I can put them in an anchored http href link.

text file example:

09:02:10 - Admin SO_Conner (1374991) teleported to player Footman_Skull_of_Reyne (1050854). 
09:02:15 - Admin SO_Conner (1374991) teleported to player Levy_Eddin_of_Reyne (1166164). 
09:02:22 - Admin SO_Conner (1374991) faded out player Levy_Eddin_of_Reyne (1166164). 
09:02:27 - Admin SO_Conner (1374991) teleported to player Valyrian_Militia_Crazymortal (1575057).

The link would look something like this:

09:02:27 - Admin SO_Conner  (<a href="?get_engine&loopup=1374991">1374991</a>) teleported to player Valyrian_Militia_Crazymortal (<a href="?get_engine&loopup=1575057">1575057</a>)

I have already tried doing it with this:

<?php

    if(strpos($line,'1374991') === false)

?>

But I can't seem to find a way to make it work.

Upvotes: 2

Views: 78

Answers (2)

ksbg
ksbg

Reputation: 3284

Using preg_replace:

$file = file(/* path to file */);
foreach($file as &$line) {
    $line = preg_replace('/(\d\d:\d\d:\d\d - [a-zA-Z _]+\()(\d+)(\)[a-zA-Z _]+\()(\d+)(\).*)/', '$1<a href="?get_engine&loopup=$2">$2</a>$3<a href="?get_engine&loopup=$4">$4</a>$5', $line);
}

This should work, even though the regex pattern could be simpler. If you are sure that the structure of every line of the txt file is always the same, you could use the following pattern instead:

$line = preg_replace('/\((\d+)\)/', '<a href="?get_engine&loopup=$1">$1</a>', $line);

Upvotes: 2

jonny
jonny

Reputation: 3098

Use this regex to find the contents between the parentheses:

var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("09:02:10 - Admin SO_Conner (1374991) teleported to player Footman_Skull_of_Reyne (1050854).");

(Regex Demo)

Form the href with simple string concatenation:

var href = "?get_engine&loopup=" + matches[1];

Then add this attrbiute to the anchor element:

$("a").attr("href", href);

Upvotes: 0

Related Questions