Chandan Gupta
Chandan Gupta

Reputation: 1480

how do I print the line which contains the word from preg_match?

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!

Upvotes: 0

Views: 760

Answers (4)

Chandan Gupta
Chandan Gupta

Reputation: 1480

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

Upvotes: 0

Chandan Gupta
Chandan Gupta

Reputation: 1480

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?

Upvotes: 0

RiaD
RiaD

Reputation: 47640

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

Upvotes: 1

Aleks G
Aleks G

Reputation: 57326

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

Upvotes: 1

Related Questions