2016-01-26 2 views
-1
<?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_id и get_name с помощью метода get_block?Вставка объектов PHP в базу данных

- 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. 

Мне нужно написать эти методы (идентификатор и имя) в моей базе данных, но, как я знаю, я не могу это сделать, если бы я только смог получить текстовый блок с get_block.

Pls, спросите меня, если что-то, может быть, я просто не могу это объяснить. : D

+3

Вы бы лучше публиковали фактический код, а не псевдокод, который технически неверен – RamRaider

+0

Определите класс правильно и, возможно, мы сможем помочь вам – RiggsFolly

+0

Посмотрите '' this-> '[RTM - Начните здесь] (http: //php.net/manual/en/language.oop5.basic.php) – RiggsFolly

ответ

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() - также потерпит неудачу.

+0

Отправленный полный код. – Brock

Смежные вопросы