首页 > 解决方案 > 如何从 php 中的句子中仅获取特定字符串以将其上传到数据库

问题描述

我只想插入句子的一部分。

就我而言,我只想从数据库列中"/admop/detail?id=527"的以下行上传。CLIENT_REQUEST

"GET /admop/detail?id=527 HTTP/1.1"

我尝试像下面那样使用修剪和爆炸,但它不起作用。有人可以帮忙吗。

$statement = "INSERT INTO TEST(
                    IP_ADDRESS, REQUEST_TIME, CLIENT_REQUEST, RESPONSE_CODE, SIZEOFOBJECT,COOKIES, AUTHSCHEME, AUTHMARKET, X_REQUESTED_WITH, ENV,TANUSER, USERID) 
                    values(:IP_ADDRESS, to_date(:REQUEST_TIME, 'YYYY-MM-DD HH24:MI:SS' ),
                    trim(:CLIENT_REQUEST)** ,:RESPONSE_CODE ,:SIZEOFOBJECT, :COOKIES, :AUTHSCHEME, :AUTHMARKET, :X_REQUESTED_WITH, :ENV, :TANUSER, :USERID )";

也作为:-

爆炸('',修剪(:CLIENT_REQUEST))

<?php

class logAgent
{
    const CONFIG_FILENAME = "data_config.ini";

    private $_dbConn;
    private $_config;


    function __construct()
    {
        $this->_loadConfig();


        $this->_dbConn = oci_connect($this->_config['db_usrnm'],
            $this->_config['db_pwd'],
            $this->_config['hostnm_sid']);
    }

    public function uploadLogs(){

        //Array of all the files present in the directory
        $f = fopen($this->_config['uploadedRegistry'], 'r');
        $contents = [];
        while (FALSE !== ($row = fgetcsv($f, 1000, $this->_config['filenameTimeSeparator']))){
            $contents[] = $row[0];
        }

        $result = array_diff(scandir($this->_config['logspath']), ['.','..'], $contents);
        foreach($result as $r){
            $this->uploadLog($r);
        }
    }

    private function _loadConfig()
    {
        // Loads config
        $path = dirname(__FILE__) . '/' . self::CONFIG_FILENAME;
        $this->_config = parse_ini_file($path) ;
    }

    public function uploadLog($filename) {

        //Uploads file to database
        $filename = trim($this->_config['logspath'] . trim($filename));

        if(file_exists($filename)){
            $myfile = fopen($filename, "r");
            while(!feof($myfile)) {
                $content= fgets($myfile);
                $carray=explode($this->_config['logFomatDelimiter'],$content);
                list($IP_ADDRESS, $REQUEST_TIME , $CLIENT_REQUEST ,$RESPONSE_CODE ,$SIZEOFOBJECT, $COOKIES, $AUTHSCHEME, $AUTHMARKET, $X_REQUESTED_WITH, $ENV, $TANUSER, $USERID)=$carray;

                if (strlen(str_replace($this->_config['discardedextensions'], '', $CLIENT_REQUEST)) !== strlen($CLIENT_REQUEST)) {
                    // Found an image
                    continue;
                }
                $statement = "INSERT INTO TEST(IP_ADDRESS, 
                REQUEST_TIME, 
                CLIENT_REQUEST,
                RESPONSE_CODE,
                SIZEOFOBJECT,
                COOKIES, 
                AUTHSCHEME,
                AUTHMARKET,
                X_REQUESTED_WITH,
                ENV,
                TANUSER, 
                USERID) 
                 values(:IP_ADDRESS, to_date(:REQUEST_TIME, 'YYYY-MM-DD HH24:MI:SS' ) , :CLIENT_REQUEST ,:RESPONSE_CODE ,:SIZEOFOBJECT, :COOKIES, :AUTHSCHEME, :AUTHMARKET, :X_REQUESTED_WITH, :ENV, :TANUSER, :USERID )";

                //Preparing an Oracle statement for execution
                $compiled = oci_parse($this->_dbConn, $statement);

                //binding values to named parameters
                oci_bind_by_name($compiled, ':IP_ADDRESS', $IP_ADDRESS);
                oci_bind_by_name($compiled, ':REQUEST_TIME', $REQUEST_TIME);
                oci_bind_by_name($compiled, ':CLIENT_REQUEST', $CLIENT_REQUEST);
                oci_bind_by_name($compiled, ':RESPONSE_CODE', $RESPONSE_CODE);
                oci_bind_by_name($compiled, ':SIZEOFOBJECT', $SIZEOFOBJECT);
                oci_bind_by_name($compiled, ':COOKIES', $COOKIES);
                oci_bind_by_name($compiled, ':AUTHSCHEME', $AUTHSCHEME);
                oci_bind_by_name($compiled, ':AUTHMARKET', $AUTHMARKET);
                oci_bind_by_name($compiled, ':X_REQUESTED_WITH', $X_REQUESTED_WITH);
                oci_bind_by_name($compiled, ':ENV', $ENV);
                oci_bind_by_name($compiled, ':TANUSER', $TANUSER);
                oci_bind_by_name($compiled,':USERID', $USERID);
                //Executing statement
                oci_execute($compiled, OCI_COMMIT_ON_SUCCESS);
            }
            //closing the file
            fclose($myfile);
            $this->updateRegistry($filename);
            return TRUE;
        }
        else{
            throw new Exception("File doesnot exist");
        }
    } 

    public function sendEmail(Exception $e){

        $sent = mail($this->_config['recipients'], $this->_config['notificationSubject'], $e);
    }

    public function updateRegistry($filename)
    {

        $uploadedfilename = fopen($this->_config['uploadedRegistry'], "a");
        fwrite($uploadedfilename, basename($filename . date($this->_config['filenameTimeSeparator'] . 'Ymdhi', time())) . PHP_EOL);
    } 
}

try {
    $logAgent = new logAgent();
    $logAgent->uploadLogs();
}
catch (Exception $e) {
    $logAgent->sendEmail($e);
}
?>

标签: php

解决方案


像这样使用

$requestPath= explode(' ', trim($your_request_path))[1];

推荐阅读