Düzenli İfadeler – Regular Expression Sınıfı

Düzenli ifade fonksiyonlarını pratik olarak bu sınıfla kullanmanız işlerini kolaylaştıracaktır.

PHP
76 lines
<?php
class DuzenliIfade{
public $duzenli;
public $mod;
public $sonuc;
private $eslesenler;
private $altmaskeler;
public function __construct($duzenli=null, $mod=null){
$this->duzenli = $duzenli;
$this->mod = $mod;
}
public function match($metin, $duzenli=null, $mod=null){
$this->kural($duzenli, $mod);
$res = preg_match($this->duzenliYap(), $metin, $this->sonuc, PREG_OFFSET_CAPTURE);
$this->clear_all();
if ($res){
$this->eslesenler = new stdClass();
$this->eslesenler->text = $this->sonuc[0][0];
$this->eslesenler->pos = $this->sonuc[0][1];
$this->altmaskeler = array();
foreach ($this->sonuc as $key=>$value){
if($key){
$submask = new stdClass();
$submask->text = $value[0];
$submask->pos = $value[1];
$this->altmaskeler[] = $submask;
}
}
}
return $res;
}
public function match_all($metin, $duzenli=null, $mod=null){
$this->kural($duzenli, $mod);
$res = preg_match_all($this->duzenliYap(), $metin, $this->sonuc, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$this->clear_all();
if($res){
$this->eslesenler = array();
$this->altmaskeler = array();
foreach ($this->sonuc as $key=>$value){
$submask = array();
foreach ($value as $key1=>$value1){
if ($key1 == 0){
$mat = new stdClass();
$mat->text = $value1[0];
$mat->pos = $value1[1];
$this->eslesenler [] = $mat;
}else{
$smat = new stdClass();
$smat->text = $value1[0];
$smat->pos = $value1[1];
$submask[] = $smat;
}
}
$this->altmaskeler[] = $submask;
}
}
return $res;
}
public function replace($metin, $repl, $duzenli=null, $mod=null){
$this->kural($duzenli, $mod);
return preg_replace($this->duzenliYap(), $repl, $metin);
}
private function kural($duzenli=null, $mod=null){
if ($duzenli) $this->duzenli = $duzenli;
if ($mod) $this->mod = $mod;
}
private function duzenliYap(){
return "/".str_replace("/", "\/", $this->duzenli)."/".$this->mod;
}
private function clear_all(){
if(is_array($this->eslesenler)) $this->eslesenler = null;
if(is_object($this->eslesenler)) $this->eslesenler = null;
if(is_array($this->altmaskeler)) $this->altmaskeler = null;
}
}
?>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Kullanımı

PHP
17 lines
<?php
$a=new DuzenliIfade;
$a->duzenli = "<a(.*?)>(.*?)</a>";
$metin='<a href="test.php">merhaba</a>';
if($a->match($metin)){
print_r($a->sonuc);
}
$a->sonuc=null;
$metin='<a href="test1.php">merhaba1</a><a href="test2.php">merhaba2</a>';
if($a->match_all($metin)){
print_r($a->sonuc);
}
$a->sonuc=null;
$repl="$1 => $2 yazısına ait.";
$metin='<a href="test1.php">merhaba1</a><a href="test2.php">merhaba2</a>';
print_r($a->replace($metin, $repl));
?>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Çıktısı

Markdown
69 lines
Array
(
[0] => Array
(
[0] => merhaba
[1] => 0
)
[1] => Array
(
[0] => href="test.php"
[1] => 2
)
[2] => Array
(
[0] => merhaba
[1] => 19
)
)
Array
(
[0] => Array
(
[0] => Array
(
[0] => merhaba1
[1] => 0
)
[1] => Array
(
[0] => href="test1.php"
[1] => 2
)
[2] => Array
(
[0] => merhaba1
[1] => 20
)
)
[1] => Array
(
[0] => Array
(
[0] => merhaba2
[1] => 32
)
[1] => Array
(
[0] => href="test2.php"
[1] => 34
)
[2] => Array
(
[0] => merhaba2
[1] => 52
)
)
)
href="test1.php" => merhaba1 yazısına ait. href="test2.php" => merhaba2 yazısına ait.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX