在txt文件中过滤行并将它们设为http href链接 [英] Filter lines in txt files and make them http href links

查看:101
本文介绍了在txt文件中过滤行并将它们设为http href链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这次要做的是得到之间的数字,将它们过滤掉我可以将它们放入锚定的http href链接。



文本文件示例:

  09:02:10  - 管理员SO_Conner(1374991)传送给玩家Footman_Skull_of_Reyne(1050854)。 
09:02:15 - 管理员SO_Conner(1374991)传送给玩家Levy_Eddin_of_Reyne(1166164)。
09:02:22 - Admin SO_Conner(1374991)淡出球员Levy_Eddin_of_Reyne(1166164)。
09:02:27 - 管理员SO_Conner(1374991)传送给玩家Valyrian_Militia_Crazymortal(1575057)。

链接如下所示:



< (< a href =?get_engine& loopup = 1374991> 1374991< / a>)传送给玩家Valyrian_Militia_Crazymortal(< a href =?get_engine& loopup = 1575057> 1575057< / a>)

这样做:

 <?php 

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

?>

但我似乎无法找到一种方法来实现它。

解决方案

使用 preg_replace

  $ file = 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);
}

即使正则表达式模式可能更简单,这也应该起作用。如果您确定txt文件的每一行结构始终相同,则可以使用以下模式:

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


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.

解决方案

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

这篇关于在txt文件中过滤行并将它们设为http href链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆