如何打印包含 preg_match 中的单词的行?

发布于 2024-11-28 03:41:23 字数 533 浏览 0 评论 0原文

mysql 表“server_var_dump”中的字段包含许多详细信息,包括站点访问者的 USER_AGENT。现在我只需要包含行的 USER_AGENTs 。现在我编写了以下脚本来匹配输出字符串的 USER_AGENT。 现在我只需要打印包含 USER_AGENT 的行

$result = mysql_query("SELECT server_var_dump FROM pageviews LIMIT 0,10");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    //printf("%s", $row["server_var_dump"]);
    if(preg_match("/[USER_AGENT]/", $row["server_var_dump"])){
    echo "match found.";
    }
    else
    echo "No matches found";     
}

请建议我如何打印包含 USER_AGENT 的行?

谢谢!

A field in mysql table "server_var_dump" which contains many details including USER_AGENT of the visitors of site. Now I only need USER_AGENTs containing line. Now I have written following script to match the USER_AGENT of the output string.
Now I need to print only those lines which contains the USER_AGENT

$result = mysql_query("SELECT server_var_dump FROM pageviews LIMIT 0,10");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    //printf("%s", $row["server_var_dump"]);
    if(preg_match("/[USER_AGENT]/", $row["server_var_dump"])){
    echo "match found.";
    }
    else
    echo "No matches found";     
}

Please suggest me how do I print the line which contains USER_AGENT?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水晶透心 2024-12-05 03:41:23

一旦得到行就打印它有什么问题吗?

$match_found = false;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if(preg_match("/[USER_AGENT]/", $row["server_var_dump"])){
        echo $row["server_var_dump"];
        $match_found = true;
        break;
    }
}
if(!$match_found) {
    echo "No matches found";
}

或者,您需要像这样获取匹配的值:

$match_found = false;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $matches = array();
    if(preg_match("/[USER_AGENT](.+)/", $row["server_var_dump"], $matches)){
        echo $matches[1];
        $match_found = true;
    }
}
if(!$match_found) {
    echo "No matches found";
}

What's wrong with just printing the row once you got it?

$match_found = false;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if(preg_match("/[USER_AGENT]/", $row["server_var_dump"])){
        echo $row["server_var_dump"];
        $match_found = true;
        break;
    }
}
if(!$match_found) {
    echo "No matches found";
}

Alternatively, you'll need to get the matched values like so:

$match_found = false;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $matches = array();
    if(preg_match("/[USER_AGENT](.+)/", $row["server_var_dump"], $matches)){
        echo $matches[1];
        $match_found = true;
    }
}
if(!$match_found) {
    echo "No matches found";
}
隔纱相望 2024-12-05 03:41:23
if(preg_match("/.*\[USER_AGENT\].*/"   //don't forget escape [ and ] chars as Salaman A said
    , $row["server_var_dump"],$matches)){
    //. is every char except newline char, So,you should get all string
    echo "match found.";
    //use $matches[0]
}
else
    echo "No matches found";

例子:

preg_match('/.*a.*/',"bc\nbac\nxy",$m);
print($m[0]); //prints bac
if(preg_match("/.*\[USER_AGENT\].*/"   //don't forget escape [ and ] chars as Salaman A said
    , $row["server_var_dump"],$matches)){
    //. is every char except newline char, So,you should get all string
    echo "match found.";
    //use $matches[0]
}
else
    echo "No matches found";

Example:

preg_match('/.*a.*/',"bc\nbac\nxy",$m);
print($m[0]); //prints bac
压抑⊿情绪 2024-12-05 03:41:23

o/p 的 ok 部分是这样的: http://pastebin.com/e1qHG64Q
当我给

$result = mysql_query("SELECT server_var_dump FROM pageviews LIMIT 0,10");
 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$found = false;
$lines = explode("\n", $row["server_var_dump"]);
// $lines = explode("\\n", $row["server_var_dump"]);
for ($x = 0; $x < count($lines); $x++) {
    if(preg_match("/[HTTP_USER_AGENT]/", $row["server_var_dump"])==TRUE)
    //if (strstr($lines[$x], "[USER_AGENT]")==TRUE)
     {
        echo $lines[$x];
        $found = true;
        break;
    }
}

它时,它给我像数组数组数组数组一样的o/p
当按照建议给出 strstr 时,它会给出类似数组的 o/p ( 数组 ( 数组 (
。用 /n 爆炸 o/p 可能不起作用..我可以做任何其他黑客来从中获取 USER_AGENT 行吗?

ok part of o/p is something like this: http://pastebin.com/e1qHG64Q
when I give

$result = mysql_query("SELECT server_var_dump FROM pageviews LIMIT 0,10");
 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$found = false;
$lines = explode("\n", $row["server_var_dump"]);
// $lines = explode("\\n", $row["server_var_dump"]);
for ($x = 0; $x < count($lines); $x++) {
    if(preg_match("/[HTTP_USER_AGENT]/", $row["server_var_dump"])==TRUE)
    //if (strstr($lines[$x], "[USER_AGENT]")==TRUE)
     {
        echo $lines[$x];
        $found = true;
        break;
    }
}

it gives me o/p like array array array array
and when the strstr is given as suggested it gives o/p like array ( array ( array (
. exploding the o/p with /n is probably not working.. any other kinda hack I can do to get just the USER_AGENT line from it?

薄情伤 2024-12-05 03:41:23

成功了! ;)

$result = mysql_query("SELECT server_var_dump FROM pageviews LIMIT 0,10");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$found = false;
$data=str_replace("=>"," ",$row["server_var_dump"]);
$lines = explode("\n", $data);

// $lines = explode("\\n", $row["server_var_dump"]);
    str_replace("\n"," ",$row["server_var_dump"]);
for ($x = 0; $x < count($lines); $x++) {
    if(preg_match("[HTTP_WAP_CONNECTION]", $lines[$x])==TRUE){
    //if (strstr($lines[$x], "[USER_AGENT]")==TRUE)
     //{
            //if(strpos($lines[$x],"[HTTP_USER_AGENT]")){
                    //echo substr($lines[$x],18);
    $y=$x-1;
    echo "$lines[$y] \n";
    }

got the hack! ;)

$result = mysql_query("SELECT server_var_dump FROM pageviews LIMIT 0,10");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$found = false;
$data=str_replace("=>"," ",$row["server_var_dump"]);
$lines = explode("\n", $data);

// $lines = explode("\\n", $row["server_var_dump"]);
    str_replace("\n"," ",$row["server_var_dump"]);
for ($x = 0; $x < count($lines); $x++) {
    if(preg_match("[HTTP_WAP_CONNECTION]", $lines[$x])==TRUE){
    //if (strstr($lines[$x], "[USER_AGENT]")==TRUE)
     //{
            //if(strpos($lines[$x],"[HTTP_USER_AGENT]")){
                    //echo substr($lines[$x],18);
    $y=$x-1;
    echo "$lines[$y] \n";
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文