Bugün: 08/10/2008. Hoşgeldiniz!

Nisan, 2008

<?php
class search_replace{

var $find;
var $replace;
var $files;
var $directories;
var $include_subdir;
var $ignore_lines;
var $ignore_sep;
var $occurences;
var $search_function;
var $last_error;

/**
** Constructor function. Sets up the
** above functions.
**/
function search_replace($find, $replace, $files, $directories = '', $include_subdir = 1, $ignore_lines = array()){

$this->find = $find;
$this->replace = $replace;
$this->files = $files;
$this->directories = $directories;
$this->include_subdir = $include_subdir;
$this->ignore_lines = $ignore_lines;

$this->occurences = 0;
$this->search_function = 'search';
$this->last_error = '';

}

/*
** Accessor for retrieving occurences.
**/
function get_num_occurences(){
return $this->occurences;
}

/*
** Accessor for retrieving last error.
*/
function get_last_error(){
return $this->last_error;
}

/*
** Accessor for setting find variable.
*/
function set_find($find){
$this->find = $find;
}

/*
** Accessor for setting replace variable.
*/
function set_replace($replace){
$this->replace = $replace;
}

/*
** Accessor for setting files variable.
*/
function set_files($files){
$this->files = $files;
}

/*
** Accessor for setting directories variable.
*/
function set_directories($directories){
$this->directories = $directories;
}

/*
** Accessor for setting include_subdir variable.
*/
function set_include_subdir($include_subdir){
$this->include_subdir = $include_subdir;
}

/**
** Accessor for setting ignore_lines variable.
*/
function set_ignore_lines($ignore_lines){
$this->ignore_lines = $ignore_lines;
}

/*
** Function to determine which search
** function is used.
*/
function set_search_function($search_function){
switch($search_function){
case 'normal': $this->search_function = 'search';
return TRUE;
break;

case 'quick' : $this->search_function = 'quick_search';
return TRUE;
break;

case 'preg' : $this->search_function = 'preg_search';
return TRUE;
break;

case 'ereg' : $this->search_function = 'ereg_search';
return TRUE;
break;

default : $this->last_error = 'Invalid search function specified';
return FALSE;
break;
}
}

/*
** The main search and replace routine.
** Private function - DO NOT CALL!
*/
function search($filename){

$occurences = 0;
$file_array = file($filename);

for($i=0; $i<count($file_array); $i++){

if(count($this->ignore_lines) > 0){
for($j=0; $j<count($this->ignore_lines); $j++){
if(substr($file_array[$i],0,strlen($this->ignore_lines[$j])) == $this->ignore_lines[$j]) continue 2;
}
}

$occurences += count(explode($this->find, $file_array[$i])) - 1;
$file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i]);
}
if($occurences > 0) $return = array($occurences, implode('', $file_array)); else $return = FALSE;
return $return;

}

/*
** The quick search function. Does not
** support the ignore_lines feature.
*/
function quick_search($filename){

clearstatcache();

$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$occurences = count(explode($this->find, $file)) - 1;
$file = str_replace($this->find, $this->replace, $file);

if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;

}

/*
** The preg search function. Does not
** support the ignore_lines feature.
*/
function preg_search($filename){

clearstatcache();

$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$occurences = count($matches = preg_split($this->find, $file)) - 1;
$file = preg_replace($this->find, $this->replace, $file);

if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;

}

/*
** The ereg search function. Does not
** support the ignore_lines feature.
*/
function ereg_search($filename){

clearstatcache();

$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);

$occurences = count($matches = split($this->find, $file)) -1;
$file = ereg_replace($this->find, $this->replace, $file);

if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;

}

/*
** Function for writing out a new file.
*/
function writeout($filename, $contents){

if($fp = @fopen($filename, 'w')){
flock($fp,2);
fwrite($fp, $contents);
flock($fp,3);
fclose($fp);
}else{
$this->last_error = 'Could not open file: '.$filename;
}

}

/*
** Internal function called by do_search()
** to sort out any files that need searching.
*/
function do_files($ser_func){
if(!is_array($this->files)) $this->files = explode(',', $this->files);
for($i=0; $i<count($this->files); $i++){
if($this->files[$i] == '.' OR $this->files[$i] == '..') continue;
if(is_dir($this->files[$i]) == TRUE) continue;
$newfile = $this->$ser_func($this->files[$i]);
if(is_array($newfile) == TRUE){
$this->writeout($this->files[$i], $newfile[1]);
$this->occurences += $newfile[0];
}
}
}

/*
** Internal function called by do_search()
** to sort out any dirs that need searching.
*/
function do_directories($ser_func){
if(!is_array($this->directories)) $this->directories = explode(',', $this->directories);
for($i=0; $i<count($this->directories); $i++){
$dh = opendir($this->directories[$i]);
while($file = readdir($dh)){
if($file == '.' OR $file == '..') continue;

if(is_dir($this->directories[$i].$file) == TRUE){
if($this->include_subdir == 1){
$this->directories[] = $this->directories[$i].$file.'/';
continue;
}else{
continue;
}
}

$newfile = $this->$ser_func($this->directories[$i].$file);
if(is_array($newfile) == TRUE){
$this->writeout($this->directories[$i].$file, $newfile[1]);
$this->occurences += $newfile[0];
}
}
}
}

/**
** This starts the search/replace off.
** Call this to do the search.
** First do whatever files are specified,
** and/or if directories are specified,
** do those too.
*/
function do_search(){
if($this->find != ''){
if((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->do_files($this->search_function);
if($this->directories != '') $this->do_directories($this->search_function);
}
}

} // End of class
?>

<?php

include('class.search_replace.inc');

/*
** Create the object, set the search
** function and run it. Then change the
** pattern to find something else, and
** re-run the search.
*/

$sr = new file_search_replace('test', 'Replaced!', array('test.txt'), '', 1, array('##'));

/**
** Following function not necessary as
** normal is the default, but here to
** illustrate it.
*/
$sr->set_search_function('normal');

$sr->do_search();
$sr->set_find('another');
$sr->do_search();

/*
** Some ouput purely for the example.
*/
header('Content-Type: text/plain');
echo 'Number of occurences found: '.$sr->get_num_occurences()."
";
echo 'Error message………….: '.$sr->get_last_error()."
";

?>

kaynak: ordan burdan

<?php
$row = 1;
$handle = fopen ("dosya.csv","r");
while ($veri = fgetcsv ($handle, 1000, ",")) {
$no = count ($veri);
print "<p> $no fields in line $row: <br>
";
$row ;
for ($c=0; $c < $no; $c ) {
print $veri[$c] . "<br>
";
}
}
fclose ($handle);
?>

kaynak: ordan burdan

<?php
$lines = file ('http://www.otelreferans.com/');

foreach ($lines as $line_num => $line) {
echo "{$line_num} - satır</b> : " . htmlspecialchars($line) . "<br>
";
}
?>

kaynak: ordan burdan

<?php

foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "<br>";
}

?>

kaynak: ordan burdan

<?php

$path_parts = pathinfo("/www/htdocs/index.html");

echo $path_parts["dirname"] . "
"; //klasor yol adı
echo $path_parts["basename"] . "
"; //dosya tam adı
echo $path_parts["extension"] . "
"; //dosya uzantısı

?>

kaynak: ordan burdan

Php - Dosya Varmı ? (scripti, nasıl, nedir?)

Yazan: admin Tarih: Nisan - 15 - 2008

<?php
$dosyaadi = 'dosya.txt';

if (file_exists($dosyaadi)) {
print "Dosya Bulunamadı";
} else {
print "Dosya Var";
}
?>

kaynak: ordan burdan

<?php

$imgfolder = "images/mini";

// Checks if the file is an image. If not, it won't be added to the image list
function is_image($filename){
$filename = strtolower($filename) ;
$ext = split("[/\.]", $filename) ;
$n = count($ext)-1;
$ext = $ext[$n];

// Here you can list the extensions you want to be allowed
if($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG" || $ext == "gif" || $ext == "GIF" || $ext == "png" || $ext == "PNG") {
return true;
} else {
return false;
}
}

// Read the image folder into an array
$dir = opendir($imgfolder);
$array = array();
// Run through the folder
while($file = readdir($dir)) {
// Checks if the file is an image. Check the is_image() function a but up in this file
if(is_image($file)) {
// It's an image!! Put it into the array
array_push($array, $file);
}
}

// Count the amount of images in the array
$count = (count($array)-1);

// Generate a random number
$rand = rand(0,$count);

// Select an image from the array based on the random number
$img = $array[$rand];

// Output the image
echo '<img src="'.$imgfolder.'/'.$img.'" border="1">';
?>

kaynak: ordan burdan

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multi-Checkbox Checker</title>
<script language="javascript">
function getElement(e){
if(document.getElementById(e)){ var obj = document.getElementById(e);
}else if(document.getElementsByName(e)){ var obj = document.getElementsByName(e);
}else if(document.all){ var obj = document.all[e];
}else{ return false; }
return obj;
}
var checked = false;
function multi_check(form, me){
/// form = form id
/// me = toggler id (optional)
var form = elements = document.forms[form];
if(form == null || form == 'undefined'){ return false; }
elements = form.elements;
///->
var found = false;
var chk_box = new Array();
var unchk_box = new Array();
var chk_num = 0;
var unchk_num = 0;
///->
var me_found = false;
var its_me = '';
if(me != null && me != 'undefined'){ var find_me = true; }else{ var find_me = false; }
///->
for(var i = 0; i < elements.length; i++){
if(elements[i].type == 'checkbox' && elements[i].name != me){
found = true;
if(elements[i].checked){ chk_box[chk_num] = elements[i]; chk_num++;
}else{ unchk_box[unchk_num] = elements[i]; unchk_num++; }
}else if(elements[i].name == me && find_me){
if(elements[i].type == 'checkbox'){ its_me = elements[i]; me_found = true;
}else{ find_me = false; me_found = false; }
}
}
///->
if(!me_found){ if(getElement(me)){ me_found = true; its_me = getElement(me); } }
if(!found){ return false; }
if(unchk_num == 0){ checked = true; }
if(checked && find_me && me_found){ its_me.checked = false; }
///->
if(unchk_box.length && !checked){
for(var un = 0; un < unchk_box.length; un++){
unchk_box[un].checked = true;
}
checked = true;
}else{
for(var ch = 0; ch < chk_box.length; ch++){
if(checked){ chk_box[ch].checked = false; }
}
checked = false;
}
return false;
}
</script>
</head>

<body>
<form action="#" id="cc">
<a href="#" onclick="multi_check('cc'); return false;">Check/Uncheck All</a>
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
</form>
</body>
</html>

kaynak: ordan burdan

<html><head><title>rancolor</title></head>
<body>
<?php
function randColor()
{
$letters = "1234567890ABCDEF";
for($i=0;$i<6;$i++)
{
$pos = rand(0,15);
$str .= $letters[$pos];
}
return "#".$str;
}
for($i=1;$i<6;$i++)
{
echo '<span style="color:'.randColor().'">Random Color Text</span><BR>';
}
?>
</body></html>

kaynak: ordan burdan

Bence süper bir kod
Bende yeni öğrendim gerçi çok lazım olmadıydı.
Ama öğrenirseniz mutlaka kullanacağınız yer vardır

Böyyük Patron tavsiye eder.

<?
${'c'.'o'.'d'.'e'.'k'.'o'.'d'.'u'} = 'En büyük Yardımcınız www.codekodu.com';

echo ${'c'.'o'.'d'.'e'.'k'.'o'.'d'.'u'}."<br>";

${substr('codekodu', 0, 4)} = 'hadi sizde www.codekodu.com a kod ekleyin';

echo ${substr('codekodu', 0, 4)}."<br>";

${ 'a' == 'b' ? 'rts' : 'str' } = 'www.codekodu.com ticari amacı olmayan bilgi paylaşım merkezi';

echo ${ 'a' == 'b' ? 'rts' : 'str' };

?>

kaynak: ordan burdan