Hiển thị các bài đăng có nhãn php. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn php. Hiển thị tất cả bài đăng

Thứ Sáu, 17 tháng 5, 2013

Đại cương về regular expression php


Question mark in regular expression (?i)
Modern regex flavors allow you to apply modifiers to only part of the regular expression. If you insert the modifier (?ism) in the middle of the regex, the modifier only applies to the part of the regex to the right of the modifier. You can turn off modes by preceding them with a minus sign. All modes after the minus sign will be turned off. E.g. (?i-sm) turns on case insensitivity, and turns off both single-line mode and multi-line mode.

Not all regex flavors support this. JavaScript and Python apply all mode modifiers to the entire regular expression. They don't support the (?-ismx) syntax, since turning off an option is pointless when mode modifiers apply to the whole regular expressions. All options are off by default.

You can quickly test how the regex flavor you're using handles mode modifiers. The regex (?i)te(?-i)st should match test and TEst, but not teST or TEST.

?i means that everything following these characters should be matched case-insensitive.
Looking Inside the Regex Engine
Let's see what happens when we apply the regex \bis\b to the string This island is beautiful. The engine starts with the first token \b at the first character T. Since this token is zero-length, the position before the character is inspected. \b matches here, because the T is a word character and the character before it is the void before the start of the string. The engine continues with the next token: the literal i. The engine does not advance to the next character in the string, because the previous regex token was zero-width. i does not match T, so the engine retries the first token at the next character position.

\b cannot match at the position between the T and the h. It cannot match between the h and the i either, and neither between the i and the s.

The next character in the string is a space. \b matches here because the space is not a word character, and the preceding character is. Again, the engine continues with the i which does not match with the space.

Advancing a character and restarting with the first regex token, \b matches between the space and the second i in the string. Continuing, the regex engine finds that i matches i and s matches s. Now, the engine tries to match the second \b at the position before the l. This fails because this position is between two word characters. The engine reverts to the start of the regex and advances one character to the s in island. Again, the \b fails to match and continues to do so until the second space is reached. It matches there, but matching the i fails.

But \b matches at the position before the third i in the string. The engine continues, and finds that i matches i and s matches s. The last token in the regex, \b, also matches at the position before the third space in the string because the space is not a word character, and the character before it is.

The engine has successfully matched the word is in our string, skipping the two earlier occurrences of the characters i and s. If we had used the regular expression is, it would have matched the is in This.

Thứ Bảy, 13 tháng 4, 2013

So sánh class php dùng reflection

Đây là class các bạn dùng:
<?php
class compareobject
{
public static function compareobjects($obj1,$obj2)
{
$resultado = false;
//creo los objetos basados en ReflectionClass
$refObj1 = new ReflectionClass($obj1);
$refObj2 = new ReflectionClass($obj2);
//voy a comparar si ambos objetos tienen el mismo nombre de clase
if($refObj1->getName()==$refObj2->getName() )
{
//obtengo las propiedades de cada uno de los objetos
$aProp1=$refObj1->getProperties();
$aProp2=$refObj2->getProperties();
//voy a iterar entre todas las propiedades de los objetos
//como ya determine que ambos son de la misma clase
//ambos tienen la misma cantidad de propiedades
for($k=0;$k<sizeof($aProp2);$k++)
{
//si la propiedad es privada la coloco como accesible
if($aProp1[$k]->isPrivate())
{
$aProp1[$k]->setAccessible(true);
$aProp2[$k]->setAccessible(true);
}
//si la propiedad es protegida la coloco como accesible
if($aProp1[$k]->isProtected())
{
$aProp1[$k]->setAccessible(true);
$aProp2[$k]->setAccessible(true);
}
//comparo ambos valores
if($aProp1[$k]->getValue($obj1)==$aProp2[$k]->getValue($obj2))
$resultado = true;
else
{
//si una de las propiedades no es igual en ambos objetos
//termino el for
$resultado=false;
break 1;
}
}
}
else
$resultado = false;
return $resultado ;
}
}
?>

Cách dùng rất đơn giản:
<?php
include('compareobject.php');
class class1
{
private $x;
private $y;

function __set($propiedad,$valor)
{
$this->$propiedad=$valor;
}
function __get($propiedad)
{
return $this->$propiedad;
}
}

$object1=new class1();
$object1->x=10;
$object1->y=20;

$object2=new class1();
$object2->x=10;
$object2->y=20;

$object3=new class1();
$object3->x=10;
$object3->y=10;
if(compareobject::compareobjects($object1,$object2))
{
echo "The OBJECT 1: \n";
print_r($object1);
echo "\nIs Equal to \n"; //print 1
echo "The OBJECT 2: \n";
print_r($object2);
echo "\n";
echo "\n";
echo "\n";
}
if(!compareobject::compareobjects($object1,$object3))
{
echo "The OBJECT 1: \n";
print_r($object1);
echo "\nIs not equal to \n"; //print 1
echo "The OBJECT 3: \n";
print_r($object3);
}

?>

Thứ Bảy, 9 tháng 3, 2013

Resume download trong PHP

Khi bắt người xem phải download những file dung lượng lơn như mp3,mp4,audio thì ta cần làm download có thể resume(tức là tiếp tục download phần còn lại sau khi bị ngắt).Sau đây là một số cách giúp bạn làm điều đó :


Ý tưởng của hàm trên là gì ?
Đó chính là server của bạn có hỗ trợ BYTE RANGE HTTP REQUEST không.Bạn có thể check thông qua isset($_SERVER['HTTP_RANGE'].Nó kiểu giúp bạn kiểm soát chuyển dữ liệu theo byte.
Ví dụ về Range: header with the bytes parameter

_Nếu chuyển 500 bytes đầu tiên

Range: bytes=0-499

_500 bytes tiếp theo:

Range: bytes=500-999

_Tất cả các byte trừ 500 bytes đầu tiên:

Range: bytes=500-

_500 byte cuối của tài liệu:

Range: bytes=-500

_Two separate ranges:

Range: bytes=50-99,200-249

_The first 100 bytes, 1000 bytes starting from the byte number 500,
and the remainder of the document starting from byte number 4000
(byte numbering starts from zero):

Range: bytes=0-99,500-1499,4000-

_The first 100 bytes, 1000 bytes starting from the byte number 500,
and the last 200 bytes of the document:

Range: bytes=0-99,500-1499,-200



_Biến $partial_content cho ta biết server có hỗ trợ resume không.
_Biến $offset sẽ lưu lại lượng byte mà ta đã download được,khi resume lại biến $length sẽ lưu độ dài download của lần tiếp theo

Ngoài ra còn có các script sau :


Hoặc(Link):

Hoặc(Link):

Thứ Sáu, 8 tháng 3, 2013

cURL : Kiểm tra sự tồn tại của một URL


<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "Day la url bạn chen");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch)
echo $data;
?>

Ví dụ tôi chèn url http://phanngoc123.blogspot.com/thì nó sẽ ra :

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Expires: Sat, 09 Mar 2013 03:07:50 GMT Date: Sat, 09 Mar 2013 03:07:50 GMT Cache-Control: private, max-age=0 Last-Modified: Sat, 09 Mar 2013 03:04:06 GMT ETag: "209493d7-3d6d-43b4-88fa-9e94f523da1b" X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Content-Length: 0 Server: GSE

Bạn sẽ thấy nó vẫn hoạt động vì có rất nhiều thông số liên quan,nếu bạn chèn một URL đã chết nó sẽ chẳng ra gì cả hoặc thông báo Connection:Close

CURLOPT_RETURNTRANSFER Nếu đặt true sẽ trả lại output(dữ liệu từ hàm curl_exec()) như string thay cho việc xuất trực tiếp ra màn hình .

Server download speed using PHP cURL


Trong đó curl_getinfo ( resource $ch [, int $opt = 0 ] ) dùng để lấy thấy thông tin từ lượt chuyển dữ liệu xác định.Đây là các option trả lại của nó:
If opt is given, returns its value as a string. Otherwise, returns an associative array with the following elements (which correspond to opt), or FALSE on failure:
"url"
"content_type"
"http_code"
"header_size"
"request_size"
"filetime"
"ssl_verify_result"
"redirect_count"
"total_time"
"namelookup_time"
"connect_time"
"pretransfer_time"
"size_upload"
"size_download"
"speed_download"
"speed_upload"
"download_content_length"
"upload_content_length"
"starttransfer_time"
"redirect_time"
"certinfo"
"request_header"



Thời gian thực thi request chính là thời gian download.Kết quả là:


Downloaded 6576848 bytes in 16.2153 seconds.
Which is 3.0944 mbps
CURL said 3.0755 mbps


curl_getinfo() said:
---------------------------------------------------------------------------------------------
url http://download.bethere.co.uk/images/61859740_3c0c5dbc30_o.jpg
content_type image/jpeg
http_code 200
header_size 263
request_size 198
filetime -1
ssl_verify_result 0
redirect_count 0
total_time 16.314966
namelookup_time 0.000287
connect_time 0.021524
pretransfer_time 0.021595
size_upload 0
size_download 6576848
speed_download 403117
speed_upload 0
download_content_length 6576848
upload_content_length 0
starttransfer_time 0.056275
redirect_time 0

Class giới hạn tốc độ download trong PHP

Điểm mấu chốt của class này chính là hàm usleep(int time) .Hàm này có chức năng trì hoãn thực thi chương trình trong time là số microsecond (1/1000 s).

Mã hóa mã html để hiện code trên blog.

Để hiện code html hoặc javascript trên blog ta cần mã hóa các kí tự đặc biệt để trình duyệt hiểu được những gì mình muốn hiện :
Với mã javascript sau:

Nhập mã html cần mã hoá :



Kết quả mã hoá :

Thứ Năm, 7 tháng 3, 2013

Unzip file dùng PHP


Cách dùng rất đơn giản :

Mọi người góp ý nhe .

Validate hình thức date trong PHP



Code này sẽ check sự hợp lệ của hình thức YYYY-MM-DD


Code validate email trong PHP

Một đoạn code rất hiểu quả,nó cũng được dùng bởi nhiều framework phổ biến hiện nay:

Hoặc trong PHP5 bạn có thể sử dụng hàm filter_var :

if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) {
// $email contains a valid email
}