将PHP对象插入数据库

时间:2016-01-26 08:56:59

标签: php

<?php
 class Prof {
    function get_block($lines, $prof_num) {
    $reserve = 200;
    $strings = implode("", $lines);
    $start_line = preg_grep("/1\.$prof_num\.1\./m", $lines);
    $prof_num+=1;
    $end_line = preg_grep("/1\.$prof_num\.1\./m", $lines);
    $prof_num-=1;
    $start_pos = mb_strpos($strings, end($start_line)); 
    $end_pos = mb_strpos($strings, end($end_line));
    $finalContent = mb_substr($strings, $start_pos-$reserve, $end_pos-$start_pos+$reserve);
    $from = "1.$prof_num";
    $prof_num+=1;
    $to = "1.$prof_num";
    $cutted = mb_substr($finalContent, strpos($finalContent, $from), strpos($finalContent, $to));
    $cutted = preg_replace("/$to\..*/", "", $cutted);
//        echo $cutted;
//        $this->get_id($cutted);
//        $this->get_name($cutted);
}

function get_id($txt) { //gettind id
    $txt = explode("\n", $txt);
    $id = preg_grep("/\d\.\sCode\s/", $txt);
    $id = preg_replace("/\d\.\sCode\s\–\s/","", $id);
    $id = preg_replace("/\t/", "",$id);
    $id = preg_replace("/\.\s+/", "",$id);
    foreach($id as $item) {
        echo $item ."\n";
    }
}

function get_name($txt) { //getting name
    $txt = explode("\n", $txt);
    $name = preg_grep("/\sName/", $txt);
    $name = preg_replace("/\d\.\sName\–\s/","",$name);
    $name = preg_replace("/\t/", "",$name);
    $name = preg_replace("/\.\s+/", "",$name);
    foreach($name as $item) {
        echo $item ."\n";
    }
}

Obj1 = new Prof();
Obj1 -> get_name(get_block(file('text.txt'))); //ERROR HERE!!!!!!!! 

如何通过get_block方法获取get_id和get_name?

- get_block gets me the whole text from text file, starting from line number $prof_num. 
- get_id gets me the id from the get_block text block. get_name just gets the name from 
get_block.

我需要将这些方法(id和name)写入我的数据库,但据我所知,我不能这样做,如果我只设法用get_block获取文本块。

请问我是否有事,也许我无法解释。 :d

1 个答案:

答案 0 :(得分:1)

这是错误的:

Obj1 -> get_name(get_block(file('text.txt')))

未定义函数get_block(),它是Pro类的一种方法。

所以你需要:

$Obj1->get_name($Obj1->get_block(file('text.txt')));

或者,如果是静态方法:

$Obj1->get_name(Pro::get_block(file('text.txt')));

另请注意,get_block()被定义为一个方法,它接受2个参数,看起来像一个字符串(基于名称......)和一个数字(基于名称......)。因此,只发送一个数组 - file()的返回值 - 也会失败。