Skip to content

Instantly share code, notes, and snippets.

/SearchPage.php

Created Dec 16, 2013
Embed
What would you like to do?
<?php
class SearchPage {
private $search;
private $pages;
private $result;
private $count;
public function __construct(array $pages) {
$this->pages = $pages;
}
public function setSearch($search) {
$this->search = $search;
}
private function setResult($page, $content) {
$obj = new stdClass;
$obj->page = $page;
$obj->content = $content;
$obj->name = basename($page);
$name = explode('.', basename($page));
$obj->title = $name[0];
$this->result[] = $obj;
}
private function getFileContent($page) {
ob_start();
$content = file_get_contents($page);
$content = preg_replace('/(\<\??php|\?\>)/is', '', $content);
$strip = trim(strip_tags($content));
$obj = new stdClass;
$obj->content = $content;
$obj->stripContent = $strip;
return $obj;
}
public function has($page) {
return file_exists($page);
}
public function execute() {
$result = false;
if ($this->search) {
foreach ($this->pages as $page) {
if ($this->has($page)) {
$obj = $this->getFileContent($page);
if (stristr($obj->stripContent, $this->search) == true) {
$this->setResult($page, $obj->content);
$this->count++;
if (!$result) $result = true;
}
}
}
}
return $result;
}
public function getResult() {
return $this->result;
}
public function getCount(){
return $this->count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment