Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Jaki wzorzec ? :/
Seth
post
Post #1





Grupa: Przyjaciele php.pl
Postów: 2 335
Pomógł: 6
Dołączył: 7.03.2002

Ostrzeżenie: (0%)
-----


Chce nieco przysypieszyc dzialanie skryptu, wiec mysle o przefiltrowaniu jednego ciagu przez preg_matche.
Tekst wygladac moze tak:

test1.test2.test3
lub
test1.test2.test3.*

Oczywiscie mozna dopsiac jeszcze test4.test5 itd.

Natomiast pozostale mozliwosci takie jak:

.test.test
lub
test.test.
lub
test.*.test
lub
test.test..test
lub
test.test.**
lub
*.test.test
itp.

nie wchodza w gre.

Ogolnie mowiac potrzebne mi jest to do funkcji includujacej inne pliki - cos w stylu import z Javy czy using z c#, wiec musze przefiltorawac podany string.

Jakies pomysly na wyrazenie regularne ? (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Seth
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 335
Pomógł: 6
Dołączył: 7.03.2002

Ostrzeżenie: (0%)
-----


A ja tak (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) :
[php:1:47a304b380]define( 'INCLUDE_PATH', 'include/' );

/**
* Use this function to include classes in given namespace pattern
*
* @param $nsPattern string
* @return true if succed, false otherwise
*/
function Import( $nsPattern )
{
// Filter pattern namespace
if ( preg_match( '/^[a-z0-9_]+(.[a-z0-9_]+){0,}(.*)?$/i', $nsPattern ) )
{
$nsPattern = str_replace( '.', '/', $nsPattern );

if ( preg_match( '/^(.*)/*$/', $nsPattern, $match ) )
{
$patch = INCLUDE_PATH.$match[1];

if ( $handle = opendir( $patch ) )
{
while ( false !== ( $file = readdir( $handle ) ) )
{
$i = 0;

if ( $file != '.' && $file != '..' && preg_match( '/^(.*).class.php$/', $file, $match ) )
{
$filePath = $patch."/".$file;

if ( file_exists( $filePath ) && is_file( $filePath ) && !class_exists( $match[1] ) )
{
require_once( $filePath );

$i++;
}
}
}

closedir($handle);
}

return ( $i ? 1 : 0 );
}
else
{
$className = substr( strrchr( $nsPattern, '/'), 1 );
$filePath = INCLUDE_PATH.$nsPattern.".class.php";

if ( file_exists( $filePath ) && is_file( $filePath ) && !class_exists( $className ) )
{
require_once( $filePath );

return 1;
}

return 0;
}
}

// Wrong pattern
return 0;
}[/php:1:47a304b380]

Wywolania:
Import( 'db.*' );
includuje wszystkie klasy z katalogu include_path.'db/'
(zastanawiam sie czy nie includowac rekursywnie takze z podkatalogow :?)

Importt( 'db.jakas_klasa' );
includuje klasy o nazwie jakas_klasa z katalogu db
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 19:05