邢栋博客

邢栋博客,Action博客,记录工作和生活中的点点滴滴

Mac下使用clion调试php源码

=====第一大步安装php=====

1、安装libiconv依赖

brew install libiconv

2、安装php
wget https://www.php.net/distributions/php-7.4.22.tar.gz
tar -zxvf php-7.4.22.tar.gz
cd php-7.4.23
./configure --prefix=/Users/action/soft/php7 --with-config-file-path=/Users/action/soft/php7/etc --with-iconv =/usr/local/opt/libiconv --enable-debug   
make
make install

ps:: 安装路径自己指定就好,iconv的路径写安装路径

如果再编译过程中报错且和iconv相关,查看文章末尾pkg-config相关内容,否则直接忽略


=====第二大步使用clion=====

1.==导入项目==

New CMake Project from  Sources  -> import as a new CMake project 

2.==编辑CMakeLists.txt==

内容如下 (根据自己的路径来)

set(PHP_SOURCE /Users/action/code/source_code/php-7.4.22)
include_directories(${PHP_SOURCE}/ext/bcmath)
include_directories(${PHP_SOURCE}/ext/bcmath/libbcmath/src)

......

include_directories(${PHP_SOURCE}/Zend)
include_directories(${PHP_SOURCE})
add_custom_target(makefile COMMAND make && make install WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})

这个时候 软件左上角会出现如下标识

image2021-9-2_11-52-11.png

3、==点击 Edit Configurations,配置如下:(配置文件路径根据自己的需要来)==

image2021-9-2_11-56-24.png

4、==然后在cli模式文件下 打个断点 sapi/cli/php_cli.c==

image2021-9-2_11-53-58.png

5、==点击右上角的debug按钮就可以进行操作了==

image2021-9-2_11-54-55.png



===================分割线=======================


====pkg-config相关====

==查看==
pkg-config --list-all

==brew安装的东西都可以直接出现在pkg-config列表里面==
export PKG_CONFIG_PATH=$(find /usr/local/Cellar -name 'pkgconfig' -type d | grep lib/pkgconfig | tr '\n' ':' | sed s/.$//)

==安装pkg-config==
wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.28.tar.gz (下载就复制链接去手动下载)
tar -xf pkg-config-0.28.tar.gz
cd pkg-config-0.28
./configure --with-internal-glib
make
sudo make install


====php生命周期====

http://xingdong365.com/program/345.html

php下正则匹配大字符串失败问题
问题描述:

在使用php函数preg_match匹配大字符串的时候匹配失败,而删除一半数据,则匹配成功,

解决过程:

于是在匹配结束后,调用preg_last_error()函数,查看失败原因,返回的是6,6对应的错误原因是PREG_JIT_STACKLIMIT_ERROR,

原来当字符串太大的时候,栈空间满了,直接就出错了,于是在匹配前加一下代码

ini_set('pcre.jit', 0);


解决查找的资料

1、深悉正则(pcre)最大回溯/递归限制(https://www.laruence.com/2010/06/08/1579.html)
php之stream_get_line()和fget()读取文件
php之stream_get_line函数和fget函数读取文件

1.txt内容如下
1111111111111111111112111111111111111111111211111111111111233333333333333

stream_get_line示例
<?php
$file_path = './1.txt';
$fp = fopen($file_path, 'r') or die("open file failure!");
$line = 0;
if ($fp) {
    while ($info = stream_get_line($fp, 8192, "2")) {  //以2为分隔符
        $line++;
    }
    fclose($fp); 
}
echo $line; //结果是4


fgets示例
<?php
$file_path = './1.txt';
$fp = fopen($file_path, 'r') or die("open file failure!");
$line = 0;
if ($fp) {
    while ($info = fgets($fp, 8192)) {
        $line++;
    }
    fclose($fp); 
}
echo $line; //结果是1

总结:
stream_get_line()函数与fgets()几乎相同,只是它允许除标准、标准和\r\n之外的行尾分隔符,并且不返回分隔符本身。
php实现单链表
<?php
    class node{
        //初始化变量,包括存储的内容和下一个数据的指针
        public $id = 0;
        public $data = '';
        public $next = null;
        //构造函数,设置存储内容的数据
        public function __construct($id,$nodedata){
            $this->id = $id;
            $this->data = $nodedata;
        }
    }


    class singleLink{
        public $head = '';
        public $size = 0;
        public function  insert($id,$value,$prenodeid = 0){

            $node = new node($id,$value);
            //如果是空链表,直接添加
            if($this->size == 0){
                $this->head = $node;
            }elseif($prenodeid == 0){
                //如果不是空链表,且并没有指定在某一个节点前添加,则在当前节点前添加
                $node->next = $this->head;
                $this->head = $node;
            }else{
                //在某一个节点后添加新节点
                $currentnode = $this->head;
                while($currentnode->next != null) {
                    if ($currentnode->next->id == $prenodeid) {
                        $node->next = $currentnode->next;
                        $currentnode->next = $node;
                        break;
                    }
                    $currentnode = $currentnode->next;
                }
            }

            $this->size++;

            return $this;
        }

        public function edit($id,$value){
            $flag = false;
            $current = $this->head;
            while(@$current->id != null){
                if($current->id == $id){
                    $current->data = $value;
                    $flag = true;
                    break;
                }
                $current = $current->next;
            }
            return $flag;
        }


        public function get($id=0){
            $current = $this->head;
            while(@$current->id != null){
                if($id != 0 && $current->id == $id){
                    $node = $current;
                    break;
                }else{
                    $node[] = array($current->id,$current->data);
                }
                $current = $current->next;
            }
            return $node;
        }


        public function delete($id){
            $flag = false;
            $current = $this->head;
            while(@$current->id != null){
                if($current->next->id == $id){
                    $current->next = $current->next->next;
                    $this->size--;
                    $flag = true;
                    break;
                }

                $current = $current->next;
            }
            return $flag;
        }

    }


    $linklist = new singleLink();
    $linklist->insert(1,'hello1');
    $linklist->insert(2,'hello2');
    $linklist->insert(3,'hello3');
    $linklist->insert(4,'hello4');
    $linklist->insert(5,'hello5');
    $linklist->insert(6,'hello6',2);
    $linklist->insert(7,'hello7');

    $linklist->delete(5);

    $linklist->insert(8,'hello8')->insert(9,'hello9')->insert(10,'hello10');

    echo "<pre>";
    print_r($linklist);
    print_r($linklist->get());
php使用mongo的GridFS存储文件
<?php
    //php使用mongo的GridFS存储文件
    $conn = new MongoClient();
    $db = $conn->photos;
    $collection = $db->getGridFS();

    //存储文件
    $id = $collection->storeFile('./logo22.png');

    //存储文件二进制流
//    $data = file_get_contents('./logo22.png');
//    $id = $collection->storeBytes($data,array('param'=>'logo图片'));

    //保存
    //$id = $collection->storeUpload('upfile');
    //相当于
    //$id = $collection->storeFile($_FILES['upfile']['tmp_name']);

    //读取
    $logo = $collection->findOne(array('_id'=>$id));
    header('Content-type:image/png');//输出图片头
    //var_dump($logo);
    echo $logo->getBytes();

php实现http401授权
<?php

//unset($_SERVER['PHP_AUTH_DIGEST']);

$username = 'xingdong'; //用户名
$userpass = '123456'; //面膜
$secret = 'xingdong365'; //秘钥

$realm = '401test';

$opaque = md5($secret.$_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']);

if (!isset($_SERVER['PHP_AUTH_DIGEST']) || empty($_SERVER['PHP_AUTH_DIGEST'])) {
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.uniqid().'",opaque="'.$opaque.'"');
    die;
}

$needed_parts = array(
    'nonce'    => 1,
    'nc'       => 1,
    'cnonce'   => 1,
    'qop'      => 1,
    'username' => 1,
    'uri'      => 1,
    'response' => 1
);

$data = array();
$keys = implode('|', array_keys($needed_parts));

preg_match_all('/('.$keys.')=(?:([\'"])([^\2]+?)\2|([^\s,]+))/', $_SERVER['PHP_AUTH_DIGEST'], $matches, PREG_SET_ORDER);

foreach ($matches as $m) {
    $data[$m[1]] = $m[3] ? $m[3] : $m[4];
    unset($needed_parts[$m[1]]);
}

//检测用户名
if ($data['username'] != $username){
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.uniqid().'",opaque="'.$opaque.'"');
    die('Invalid username.');
}

$password = md5($username.':'.$realm.':'.$userpass);

$response = md5($password.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']));



if ($data['response'] != $response) {
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.uniqid().'",opaque="'.$opaque.'"');
    die('Invalid password.');
}


echo "success";

php代码执行过程简述
php代码的执行过程

扫描->解析->编译->执行->输出

1.扫描(scanning) 将index.php内容变成一个个语言片段(token)
<?php

$code =<<<'PHP_CODE'
<?php
//这是注释
echo "hello world\n";
$data = 1+1;
eval("echo 'Inception lvl 1...\n';");
echo $data;
PHP_CODE;
echo "<pre>";
print_r(token_get_all($code));

echo token_name(319);//输出 T_ECHO  ,260 T_EVAL(eval不是函数)

/*
 *   [16] => Array
        (
            [0] => 319  //tokenid,也就是词的id
            [1] => echo  //词具体的内容
            [2] => 5 //所在的位置,行数
        )
 *
 */

?>

2.解析(parsing) 将一个个语言片段变成有意义的表达式
3.编译(complication) 将表达式编译成中间码(opcode)  //可以使用vld扩展或者parsekit扩展查看
4.执行(execution) 将中间码一条条的执行
如果执行的是某内置函数,会调用对应的函数;
如果调用的是扩展,则会将控制权交给这些扩展,扩展执行完成后将结果返回给 zend engine
全部执行完成后zend engine将结果返回给php内核,内核再返回给sapi,最后返回给服务器,通过服务器httpresponse返回到浏览器

5.输出(output buffer) 将要输出的内容输出到缓冲区

php之XMLReader简单事例

新建xml.xml

<?xml version="1.0" encoding="utf-8"?>  
<shows>  
    <show>  
        <name>action</name>
        <age>18</age>
        <sex>男</sex>
    </show>  
    <show>  
        <name>yiyi</name>
        <age>20</age>
        <sex>女</sex>
    </show>  
</shows>  
xml.php如下
<?php
    $items = [];
    $reader = new XMLReader();
    $reader->open('xml.xml','utf-8');

    while($reader->read()){
        if($reader->name == 'show' && $reader->nodeType == XMLReader::ELEMENT){
            $item = [];
            while($reader->read() && $reader->name != 'show'){
                if($reader->nodeType != XMLReader::ELEMENT)continue;

                $name = $reader->name;
                $value = $reader->readString();
                $item[$name]  = $value;
            }
            $items[] = $item;

        }

    }
    echo "<pre>";
    print_r($items);



优惠券
最新微语