przydatne jeśli chcemy na szybko wygenerować sobie listę wszystkich plików z katalogu i opakować w #filename
/**
* creating list.txt
* ls > list.txt
*/
//const input output file
define('__INPUT_FILE__', "list.txt");
define('__OUTPUT_FILE__', "lista.html");
//wrapper for each line of input file
define('__PRE__','<a href="');
define('__MIDDLE__','">');
define('__SUFFIX__','</a><br/>');
//read input file into array
$list = file(__INPUT_FILE__);
//open output file to write and set pointer at the end of file
//if file does no exist attempt to create one
$file = fopen(__OUTPUT_FILE__, "a+");
//write each line into file into wrapper const
foreach($list as $value)
{
$output = __PRE__ . $value . __MIDDLE__ . $value . __SUFFIX__;
fwrite($file, $output);
}
//close file after finished
fclose($file);
Mam plik z logami, z których intereuje mnie tylko część danych.
Plik z którego chcę wyciągnąć informacje “init.log” wygląda tak:
Oct 20 14:00:00 ws1-03463fc1e1c newsyslog[93689]: logfile turned over due to size&amp;amp;amp;amp;amp;amp;gt;100K
Oct 20 14:34:50 ws1-03463fc1e1c installdb[11924]: started (uid 96)
Oct 20 14:34:50 ws1-03463fc1e1c installdb[11924]: Opened receipt database on '/' with schema 17.
Oct 20 14:34:57 ws1-03463fc1e1c installdb[11924]: done. (0.006u + 0.005s)
Oct 20 15:34:50 ws1-03463fc1e1c installdb[43080]: started (uid 96)
Oct 20 15:34:50 ws1-03463fc1e1c installdb[43080]: Opened receipt database on '/' with schema 17.
Oct 20 15:34:56 ws1-03463fc1e1c installdb[43080]: done. (0.006u + 0.004s)
Oct 20 19:58:27 ws1-03463fc1e1c installdb[28727]: started (uid 96)
Oct 20 19:58:27 ws1-03463fc1e1c installdb[28727]: Opened receipt database on '/' with schema 17.
Oct 20 19:58:33 ws1-03463fc1e1c installdb[28727]: done. (0.006u + 0.006s)
Oct 20 20:58:27 ws1-03463fc1e1c installdb[58388]: started (uid 96)
Oct 20 20:58:27 ws1-03463fc1e1c installdb[58388]: Opened receipt database on '/' with schema 17.
Oct 20 20:58:33 ws1-03463fc1e1c installdb[58388]: done. (0.007u + 0.006s)
Oct 21 08:28:28 ws1-03463fc1e1c installdb[28806]: started (uid 96)
Oct 21 08:28:28 ws1-03463fc1e1c installdb[28806]: Opened receipt database on '/' with schema 17.
Oct 21 08:28:34 ws1-03463fc1e1c installdb[28806]: done. (0.007u + 0.007s)
Oct 21 09:28:27 ws1-03463fc1e1c installdb[59279]: started (uid 96)
Oct 21 09:28:27 ws1-03463fc1e1c installdb[59279]: Opened receipt database on '/' with schema 17.
Oct 21 09:28:33 ws1-03463fc1e1c installdb[59279]: done. (0.006u + 0.004s)
Oct 21 10:28:28 ws1-03463fc1e1c installdb[89960]: started (uid 96)
Oct 21 10:28:28 ws1-03463fc1e1c installdb[89960]: Opened receipt database on '/' with schema 17.
Oct 21 10:28:34 ws1-03463fc1e1c installdb[89960]: done. (0.006u + 0.006s)
Oct 21 11:28:28 ws1-03463fc1e1c installdb[20809]: started (uid 96)
Oct 21 11:28:28 ws1-03463fc1e1c installdb[20809]: Opened receipt database on '/' with schema 17.
Oct 21 11:28:34 ws1-03463fc1e1c installdb[20809]: done. (0.006u + 0.005s)
Oct 21 12:28:29 ws1-03463fc1e1c installdb[51229]: started (uid 96)
Oct 21 12:28:29 ws1-03463fc1e1c installdb[51229]: Opened receipt database on '/' with schema 17.
Oct 21 12:28:35 ws1-03463fc1e1c installdb[51229]: done. (0.006u + 0.006s)
Oct 21 13:28:28 ws1-03463fc1e1c installdb[82151]: started (uid 96)
Oct 21 13:28:28 ws1-03463fc1e1c installdb[82151]: Opened receipt database on '/' with schema 17.
Oct 21 13:28:34 ws1-03463fc1e1c installdb[82151]: done. (0.006u + 0.004s)
Oct 21 14:28:29 ws1-03463fc1e1c installdb[12290]: started (uid 96)
Oct 21 14:28:29 ws1-03463fc1e1c installdb[12290]: Opened receipt database on '/' with schema 17.
Oct 21 14:28:35 ws1-03463fc1e1c installdb[12290]: done. (0.006u + 0.005s)
Oct 21 15:28:30 ws1-03463fc1e1c installdb[43523]: started (uid 96)
Oct 21 15:28:30 ws1-03463fc1e1c installdb[43523]: Opened receipt database on '/' with schema 17.
Oct 21 15:28:36 ws1-03463fc1e1c installdb[43523]: done. (0.006u + 0.004s)
Oct 22 08:33:50 ws1-03463fc1e1c installdb[30573]: started (uid 96)
Oct 22 08:33:50 ws1-03463fc1e1c installdb[30573]: Opened receipt database on '/' with schema 17.
Oct 22 08:33:56 ws1-03463fc1e1c installdb[30573]: done. (0.006u + 0.006s)
a chcę wyciągnąć z niego linia po linii pełną datę z gofdziną oraz id akcji, np:
Oct 22 08:33:56 installdb[30573]
cała reszta mnie nie interesuje.
Tutaj każda linijka tekstu zaczyna się od daty potem trzeba przeskoczyć kilka znaków i wybrać id_akcji,
można więc wybrać znaki od 1-16, oraz od 32-48, co powinno załatwić sprawę.
tak to może wyglądać:
//wczytaj plik do tablicy
$content = file('init.log');
//wybierz potrzebne znaki z każdej linijki i przeiteruj po nich
foreach($content as $data)
{
echo substr($data, 0, 16);
echo substr($data, 32, 16);
}
wynik:
Oct 20 14:00:00 newsyslog[93689]
Oct 20 14:34:50 installdb[11924]
Oct 20 14:34:50 installdb[11924]
Oct 20 14:34:57 installdb[11924]
Oct 20 15:34:50 installdb[43080]
Oct 20 15:34:50 installdb[43080]
Oct 20 15:34:56 installdb[43080]
Oct 20 19:58:27 installdb[28727]
Oct 20 19:58:27 installdb[28727]
Oct 20 19:58:33 installdb[28727]
Oct 20 20:58:27 installdb[58388]
Oct 20 20:58:27 installdb[58388]
Oct 20 20:58:33 installdb[58388]
Oct 21 08:28:28 installdb[28806]
Oct 21 08:28:28 installdb[28806]
Oct 21 08:28:34 installdb[28806]
Oct 21 09:28:27 installdb[59279]
Oct 21 09:28:27 installdb[59279]
Oct 21 09:28:33 installdb[59279]
Oct 21 10:28:28 installdb[89960]
Oct 21 10:28:28 installdb[89960]
Oct 21 10:28:34 installdb[89960]
Oct 21 11:28:28 installdb[20809]
Oct 21 11:28:28 installdb[20809]
Oct 21 11:28:34 installdb[20809]
Oct 21 12:28:29 installdb[51229]
Oct 21 12:28:29 installdb[51229]
Oct 21 12:28:35 installdb[51229]
Oct 21 13:28:28 installdb[82151]
Oct 21 13:28:28 installdb[82151]
Oct 21 13:28:34 installdb[82151]
Oct 21 14:28:29 installdb[12290]
Oct 21 14:28:29 installdb[12290]
Oct 21 14:28:35 installdb[12290]
Oct 21 15:28:30 installdb[43523]
Oct 21 15:28:30 installdb[43523]
Oct 21 15:28:36 installdb[43523]
Oct 22 08:33:50 installdb[30573]
Oct 22 08:33:50 installdb[30573]
Oct 22 08:33:56 installdb[30573]
Wydostac informacje z pliku XML za pomocą PHP i SimpleXML jest prosta ale co jeśli chcielibyśmy zrobić edycję pliku ? Read more…
chcielibyśmy wysłać plik na serwer, hmm… dość prosta sprawa,
Read more…
Jesli chcemy w PHP zczytać konkretne dane z pliku XML to chyba najprościej zaprząść do tego SimpleXML. Dokładna dokumentacja znajduje się na php.net al poniżej przedstawiam prosty przykład.
Read more…
poniżej kodzik dla wzorca projektowego Singleton.
Singleton należy do grupy wzorców konstrukcyjnych.
Generalnie założenie wzorca to możliwość utworzenia jednego i tylko jednego obiektu danej klasy. Najprostrzy i chyba najbardziej popularny przykład wykorzystania tego wzorca to połączenie z tą samą bazą danych w obrębie jednego programu. Wystarczy aby połączyć się z bazą jedynie raz i potem konsekwentnie wykorzystywać to połączenie bez tworzenia nowego co więcej nie musimy pamiętać o tym czy już je nawiązywaliśmy wcześniej w kodzie czy nie, łatwo tu o pomyłkę i zdublowanie a nawet wielokrotne powielenie tej samej operacji. Model singleton skutecznie nas od tego chroni. Poniżej krótki kodzik połączenia z bazą:
class Connection
{
private static $DbConnect;
private function __construct() {}
public static function getConnection()
{
if(empty(self::$DbConnect))
{
self::$DbConnect = new mysqli(host, user, pass, databasename);
}
return self::$DbConnect;
}
}
wykorzystanie kodu:
$polaczenie = Connection::getConnection();
//zwraca nam obiekt połaczenia z bazą
Ponieważ klasa Connection ma zadeklarowaną $DbConnect jako static i private więc nie będzie się
do niej odwołać z zewnątrz (z poza tej klasy) np. nie będzie można napisać
$dana = Connect::$DbConnect
tak samo nie będzie można utworzyć instancji (obiektu) tej klasy poprzez
$nowainstancja = new Connetion();
przy takim wywołaniu jako pierwsza zostanie wykonana funkcja __construct ale ona z kolei jest zadeklarowana jako private więc nie można się do niej odwołać spoza klasy, próba utworzenia instancji klasy Connection z góry skazana jest na niepowodzenie.
Jak więc ją wykorzystać ?
wywołując na klasie Connection metodę getConnection() zwraca ona zmienną z statyczną self::$DbConnect , która jest połaczeniem z bazą co więcej, metoda getConnection() sprawdza czy połączenie już istnieje czyli czy istnieje (empty(self::$DbConnect)) jeśli tak zwróci resource (resource – połączenie) jeśli nie utworzy nowe połączenie z bazą i zwróci je użytkownikowi.
SPL – Standard PHP Library SPL is a collection of interfaces and classes that are meant to solve standard problems and implements some efficient data access interfaces and classes. You’ll find the classes documented using php code in the file spl.php or in corresponding .inc files in subdirectories examples and internal. Based on the internal implementations or the files in the examples subdirectory there are also some .php files to experiment with.
o SPL na php.net
poniżej klasa implementująca interfejs Iterator z Standard PHP Library
Read more…
Połączenie z bazą danych PostgreeSQL przebiega w kilku krokach:
1. połączenie z bazą danych ($polaczenie), musimy podać nazwę_hosta, nazwę_bazy_danych, z której będziemy kożystać, nazwę_urzytkownika i hasło.
Read more…
Funkcje gettext implementują NLS (Native Language Support) API, które może być użyte do umiędzynarodowienia aplikacji napisanych w PHP.
Dokumentacja gettext znajduje się tutaj
Read more…
Prosta sprawa, mam swoją stronę domową i chciałbym umieścić w niej wyszukiwarkę ‘google’. Lubię prostotę więc chciałbym aby całość składała się ze zwykłego pola wyszukiwania plus przycisku ‘szukaj’.
Read more…