Bessere Beispiele mit preg_quote():
<?php
# Tabelle angelegt mit:
# create table `test`.`kategorien`
# (`Kategorie` VARCHAR(30) NOT NULL );
# set $database, $user and $pass
include 'settings.php';
$dbh = new PDO(
"mysql:host=localhost;dbname=$database",
$user,
$pass
);
# Frisch einlesen?
#/*
$dbh -> query( 'DELETE FROM `kategorien`' );
$w500 = json_decode(
file_get_contents( '500.json' )
);
for($i=0; $i<count( $w500 ); $i++) {
$w500[$i] = '(' . $dbh->quote( $w500[$i] ) . ')';
}
$sql = "INSERT INTO `test`.`kategorien`(`Kategorie`) VALUES "
. implode(',', $w500 );
$startT = hrtime( true );
$dbh -> query( $sql );
echo "Einlesen der 500 Wörter in die Datentabelle: "
. ( hrtime( true ) - $startT ) / 1e+6 . " ms.\n";
#*/
$startT1 = hrtime( true );
$w1000 = explode( ' ', file_get_contents( '1000.txt' ) );
$startT = hrtime( true );
for($i=0; $i<count( $w1000 ); $i++) {
$regex[] = preg_quote( $w1000[$i] ) . '$';
}
$regex = implode( '|', $regex );
$regex = $dbh->quote( $regex );
$endT1 = hrtime( true );
$startT2 = hrtime( true );
$sql = "SELECT `Kategorie` FROM `test`.`kategorien`
WHERE `Kategorie` REGEXP "
. $regex;
$sth = $dbh->prepare( $sql );
$sth->execute();
$founds=[];
while ( $result = $sth->fetch( PDO::FETCH_ASSOC ) ) {
$founds[] = $result['Kategorie'];
}
$endT2 = hrtime( true );
echo "Zeit zum Erzeugen des regulären Ausdrucks: "
. ( $endT1 - $startT ) / 1e+6
. " ms.\n";
echo "Zeit zum Ermitteln der Übereinstimmungen aus der Datenbank: "
. ( $endT2 - $startT2 ) / 1e+6
. " ms.\n";
echo "Gesamtzeit: "
. ( $endT2 - $startT1 ) / 1e+6
. " ms.\n";
echo substr( $sql , 0, 150 ) . "...\n";
print_r( $founds );
<?php
class Searcher {
protected $ar;
protected $helper;
function __construct () {
$ar = json_decode( file_get_contents( '500.json' ) );
foreach ( $ar as $k ) {
$helper[$k]=true;
}
$this->ar = $ar;
$this->helper = $helper;
}
function start_with_regex( $s ) {
$pattern = '/^' . preg_quote( $s . '/' );
foreach ( $this->ar as $haystack ) {
if ( preg_match( $pattern, $haystack ) ) return true;
#if ( 0===strpos( $haystack, $s ) ) return true;
}
}
function end_with_regex( $s ) {
#$l = strlen( $s );
$pattern = '/' . preg_quote( $s . '$/' );
foreach ( $this->ar as $haystack ) {
if ( preg_match( $pattern, $haystack ) ) return true;
}
}
function contains_regex( $s ) {
$pattern = '/' . preg_quote( $s . '/' );
foreach ( $this->ar as $haystack ) {
if ( preg_match( $pattern, $haystack ) ) return true;
}
}
function contains_exact( $s ) {
if ( array_key_exists( $s, $this->helper ) ) return true;
}
function start_with_str( $s ) {
foreach ( $this->ar as $haystack ) {
if ( 0 === strpos( $haystack, $s ) ) return true;
}
}
function end_with_str( $s ) {
$l = strlen( $s );
foreach ( $this->ar as $haystack ) {
$hl = strlen( $haystack );
if ( ( $l - $hl ) === strpos( $haystack, $s ) ) return true;
}
}
function contains_str( $s ) {
foreach ( $this->ar as $haystack ) {
$p = strpos( $haystack, $s );
if ( 0 === $p OR $p ) return true;
}
}
function get_helper() {
return $this->helper;
}
}
$searcher = new Searcher();
$w1000 = explode(
' ',
file_get_contents( '1000.txt' )
);
### contains_exact
$founds = [];
$helper = $searcher->get_helper();
$startT = hrtime( true );
foreach ( $w1000 as $w ) {
if (
array_key_exists( $w, $helper )
) $founds[] = $w;
}
$endT = hrtime( true );
echo "contains_exact : "
. ( $endT - $startT ) / 1e+6
. " ms. " . count($founds) . " Funde.\n";
### contains_regex:
$founds=[];
$startT = hrtime( true );
foreach ( $w1000 as $w ) {
if (
$searcher -> contains_regex( $w )
) $founds[] = $w;
}
$endT = hrtime( true );
echo "contains_regex : "
. ( $endT - $startT ) / 1e+6
. " ms. " . count($founds) . " Funde.\n";
### contains_str:
$founds=[];
$startT = hrtime( true );
foreach ( $w1000 as $w ) {
if (
$searcher -> contains_str( $w )
) $founds[] = $w;
}
$endT = hrtime( true );
echo "contains_str : "
. ( $endT - $startT ) / 1e+6
. " ms. " . count($founds) . " Funde.\n";
### start_with_regex:
$founds=[];
$startT = hrtime( true );
foreach ( $w1000 as $w ) {
if (
$searcher -> start_with_regex( $w )
) $founds[] = $w;
}
$endT = hrtime( true );
echo "start_with_regex: "
. ( $endT - $startT ) / 1e+6
. " ms. " . count($founds) . " Funde.\n";
### start_with_str:
$founds=[];
$startT = hrtime( true );
foreach ( $w1000 as $w ) {
if (
$searcher -> start_with_str( $w )
) $founds[] = $w;
}
$endT = hrtime( true );
echo "start_with_str : "
. ( $endT - $startT ) / 1e+6
. " ms. " . count($founds) . " Funde.\n";
### end_with_regex:
$founds=[];
$startT = hrtime( true );
foreach ( $w1000 as $w ) {
if (
$searcher -> end_with_regex( $w )
) $founds[] = $w;
}
$endT = hrtime( true );
echo "end_with_regex : "
. ( $endT - $startT ) / 1e+6
. " ms. " . count($founds) . " Funde.\n";
### end_with_str:
$founds=[];
$startT = hrtime( true );
foreach ( $w1000 as $w ) {
if (
$searcher -> end_with_str( $w )
) $founds[] = $w;
}
$endT = hrtime( true );
echo "end_with_str : "
. ( $endT - $startT ) / 1e+6
. " ms. " . count($founds) . " Funde.\n";
print_r( $founds );