Tuesday, 22 March 2016

Named Subpattern

নেমড প্যাটার্ন এর নাম ইনডেক্স হিসাবে এরে তে রিটার্ন করে, এটা অনেকটা সাব প্যাটার্ন এর মত কাজ করে কিন্তু সিনটেক্স ভিন্ন। দুই ধরনের সিনটেক্স আছে এতে। 

 <?php
$str = "Year: 2016";
preg_match('/(?<Name>\w+): (?<Value>\d+)/', $str, $matches);
// OR preg_match('/(?P<Name>\w+): (?P<Value>\d+)/', $str, $matches);
print_r($matches);
?>

Output:
Array
(
    [0] => Year: 2016
    [Name] => Year
    [1] => Year
    [Value] => 2016
    [2] => 2016
)

Question Set 1

1. $pattern = "GHI";
$subject = "ABCDEFGHIJKLMN";
If you find the pattern "GHI" which is the faster way to find it?

a) preg_match()
b) strpos

Ref: http://php.net/manual/en/function.preg-match.php
==================================================

2.

Getting the domain name out of a URL

( এ ক্ষেত্রে সাবজেক্ট স্ট্রিং এর শেষ থেকে সুরু করতে হবে )

আমরা যদি এমন একটি প্যাটার্ন মেচ করাতে চাই যার শেষ অংশ অবশ্যই মেচ করতে হবে কিন্তু সেখানে কোনো ডট থাকতে পারবেনা এবং তার আগে একটি ডট থাকবে এবং তার আগে ডট ছাড়া ক্যারেক্টার
থাকবে।

// get last two segments of host name
$host = "www.php.net";
preg_match_all('/[^.]+\.[^.]+$/', $host, $matches);
print_r($matches);

/*
Array
(
    [0] => Array
        (
            [0] => php.net
        )

)

How to Pass Zend PHP 5.3 exam in first attempt?

Some of the predefined POSIX in regular expression

The usage position of metacharacters in a regular expression determines the result of the regular expression. If caret (^) metacharacter is used inside a character list, it means the negation of a character list. Therefore, [^[:digit:]] looks for a pattern consisting of any non-digit character. Therefore, in the given scenario, you will use the [^[:digit:]] regular expression to accomplish the task.

Some of the predefined POSIX (Portable Operating System Interface) character classes are as follows:
  • [:alpha:] Alphabetic characters
  • [:lower:] Lower case alphabetic characters
  • [:upper:] Upper case alphabetic characters
  • [:digit:] Numeric digits
  • [:alnum:] Alphanumeric characters
  • [:space:] Non-printing space characters
  • [:punct:] Punctuation characters
  • [:print:] Printable characters
Answer options A and D are incorrect. The caret (^) metacharacter cannot be used at the end of any regular expression.

Pattern = [^[:digit:]]

Difference between greedy and ungreedy

 Greedy এর ক্ষেত্রে :
প্রথম প্যাটার্ন পরবর্তী কোনো প্যাটার্ন কে বার বার মেচ করার সূযুগ না দিয়ে প্রথম প্যাটার্ন এগিয়ে চলবে, পরবর্তী প্যাটার্ন কে সুধু শেষ বার সুযুগ দেবে। অর্থাৎ জুর যার মুল্লুক তার।

preg_match_all('/.*day/', "saturdaysunday", $matches);
print_r($matches);
 
Output: 
Array
(
    [0] => Array
        (
            [0] => saturdaysunday
        )
)



Un-Greedy এর ক্ষেত্রে :
 প্রথম প্যাটার্ন পরবর্তী কোনো প্যাটার্ন কে বার বার মেচ করার সূযুগ  দিবে, প্রথম প্যাটার্ন এগিয়ে চলবে।
preg_match_all('/.*?day/', "saturdaysunday", $matches);
// OR preg_match_all('/.*day/U', "saturdaysunday", $matches);
print_r($matches);

Output:
Array
(
    [0] => Array
        (
            [0] => saturday
            [1] => sunday
        )
)

Sunday, 20 March 2016

Subpattern in regular expression

টোটাল প্যাটার্ন যদি ম্যাচ করে তাহলে সেই টোটাল প্যাটার্ন এবং সাব প্যাটার্ন গুলি এরে তে রিটার্ন। যদি টোটাল প্যাটার্ন এ একটাই সাব প্যাটার্ন থাকে তাহলে একটা এম্পটি স্ট্রিং ধরে টোটাল প্যাটার্ন করে এবং সেই টোটাল প্যাটার্ন এবং সাব প্যাটার্ন গুলি এরে তে রিটার্ন করে। টোটাল প্যাটার্ন যদি  ম্যাচ না করে তাহলে সাব প্যাটার্ন ও রিটার্ন করেনা।

preg_match('/saturday(sunday)/', "saturdaysunday", $matches);
print_r($matches);

/* Using subpattern (sunday)
Array
(
    [0] => saturdaysunday
    [1] => sunday
)
*/

Saturday, 19 March 2016

Non greedy/ungreedy regular expression

বাই ডিফল্ট রেগুলার এক্সপ্রেশন হয় Greedy, অর্থাৎ quantifier (.*, অন্যান্য) যত character পায় ততই consume করে যেমন xxxxzzzz, এখানে ৪ টা x এবং ৪ টা z, এখানে আমি যদি (.*)  ব্যবহার করি এটা সবগুলি character
নিয়া নিবে, যেমন এই প্যাটার্ন = "/(.*)(z+)/" সবগুলি নিতে চায় তবে যেহেতু z+ পরে ব্যবহার করা হইয়াছে তাই বেচারা z+ জন্য একটা z রেখে সবগুলু x এবং z নিয়া নেয়। তবে ইন্টারেষ্টিং হলো এখানে যদি z* ব্যবহার করা হত .*  সবগুলি character নিয়ে নিত।

<?php
/*** 4 x and 4 z chars ***/
$string = "xxxxzzzz";
/*** greedy regex ***/
preg_match("/^(.*)(z+)$/",$string,$matches);
/*** results ***/
echo $matches[1];
echo "<br />";
echo $matches[2];
?>

Friday, 18 March 2016

Look Aheads, Look Behinds regular expression


http://infoheap.com/php-look-ahead-look-behind-regex/

//Negative look ahead

//Return true if string1(hello) is not followed by string2( world)
ট্রু রিটার্ন করে যদি একটা স্ট্রিং এর পরে আরেকটা স্ট্রিং না থাকে।
যেমন hello এর পরে যদি  wold না থেকে অন্য কিছু থাকলে তবে এটি match করবে। 
(এক্ষেত্রে ওয়ার্ল্ড এর আগে একটি স্পেস আছে) 
if (preg_match('/hello(?! world)/i', "hello something else", $matches, PREG_OFFSET_CAPTURE)) {

  echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n";

} else {

  echo "No match\n";

}



//Positive look ahead

//Return true if string1(hello) is followed by string2( world)

if (preg_match('/hello(?= world)/i', "hello world", $matches, PREG_OFFSET_CAPTURE)) {

  echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n";

} else {

  echo "No match\n";

}





//Negative look behind

//Return true if string2(world) is not preceded by string1(hello )
if (preg_match('/(?<!hello )world/i', "something world", $matches, PREG_OFFSET_CAPTURE)) {

  echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n";

} else {

  echo "No match\n";

}



//Positive look behind

//Return true if string2(world) is preceded by string1(hello )

if (preg_match('/(?<=hello )world/i', "hello world", $matches, PREG_OFFSET_CAPTURE)) {

  echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n";

} else {

  echo "No match\n";

}

Wednesday, 16 March 2016

substr_compare() : compares main_str from position offset with sub_str up to length characters.

echo substr_compare("abc", "abc", 0); // 0, similar to strcmp
echo substr_compare("bbc", "abc", 0); // 1, similar to strcmp
echo substr_compare("abcd", "abc", 0); // 1, similar to strcmp
echo substr_compare("abc", "abcd", 0); // -1, similar to strcmp
echo substr_compare("abc", "bbc", 0); // -1, similar to strcmp
//If length is specified, that specified length of substr and mainstr is compared, the other chars are ignored
echo substr_compare("abc", "abcd", 0 , 3); // 0
//If specified length is more than the subsring but subsring is matched with the part of the
//mainstr, so ramining length of subsring is printed
echo substr_compare("abcdefgh", "abc", 0 , 6); // 3
\http://www.w3schools.com/php/func_string_substr_compare.asp
offset theke suru kore length porjonto main string er sathe subsring er tulona kora hoi.

<?phpecho substr_compare("abcde""bc"12); // 0echo substr_compare("abcde""de", -22); // 0echo substr_compare("abcde""bcg"12); // 0echo substr_compare("abcde""BC"12true); // 0echo substr_compare("abcde""bc"13); // 1
//echo substr_compare("abcde""cd"12); // -1
// cd ar offset deo offset chea basiecho substr_compare("abcde""abc"51); // warning?>

strcspn : Returns the length of the initial segment of subject which does not contain any of the characters in mask.

int strcspn ( string $subject , string $mask [, int $start [, int $length ]] )
Mask এর যে কোন একটা character মিললে তার আগ পর্যন্ত length রিটার্ন করে।
<?php
$a 
strcspn('abcd',  'apple');

$b strcspn('abcd',  'banana');
$c strcspn('hello''l'); 
$d strcspn('hello''world'); 
$e strcspn('abcdhelloabcd''abcd', -9);
$f strcspn('abcdhelloabcd''abcd', -9, -5);

var_dump($a);var_dump($b);var_dump($c);

var_dump($d);var_dump($e);var_dump($f); 
?>
int(0)
int(0)
int(2)
int(2)
int(5)
int(4)
 

Monday, 14 March 2016

chr and ord in respect of printf, sprintf

echo chr("65"); // A
printf("%c", "65"); //A
echo sprintf("%c", "65"); //A

echo ord("A"); // 65

bin2hex and hex2bin

bin2hex — Convert binary data into hexadecimal representation,
here BINARY means ASCII characters

$b = "Aa";
echo bin2hex($b); // 4161 , 41=A, 61=B
echo hex2bin("4161"); // Aa

addcslashes

string addcslashes ( string $str , string $charlist )
Returns a string with backslashes before characters that are listed in charlist parameter.

charlist
1) \n and \r are converted to C-style
2) 0-32 and higher than 126 converted to octal
3) A..z the range is correct, A to Z, a to z and period will be escaped
4) z..A the range is incorrect, only z,A and period(.) will be escaped
5) "\0..\37", which would escape all characters with ASCII code between 0 and 31.

echo addcslashes('foo[ ]', 'A..z'); // \f\o\o\[ \]

echo addcslashes("zoo['.']", 'z..A'); // \zoo['\.']

addslashes

addslashes escape
1) ' (single quote)
2) " (double quote)
3) \ (slash)
4) NULL byte

Which of the following global parameters was auto addslashes when magic_quote_gpc was on in PHP 5.4? (choose 3)

1. $_GET
2. $_POST
3. $_COOKIE
4. $_SERVER




Sunday, 13 March 2016

sprintf

echo sprintf("%'x8.1f", 18); // xxxx18.0, total 8 characters, .1f after point one 0 be padded, if 18.1 no 0 be padded, 'x8 means output be 8 chars, if number is 2 digit, the remaining chars be padded with 'x and after point 0 be padded.

echo $formatted = sprintf("%01.4f", .35);  // 0.3500, one 0 padded in left and 4 zero padded in right, if digits replace the zero places no zero will be shown.



echo sprintf("%10.2f", 1); // default padding space
space
space
space
space
space
space
1
.
0
0

echo sprintf("%10.2f", .1); // .1 = 0.1
space
space
space
space
space
space
0
.
1
0

echo sprintf("%010.2f", .1); // no need single quote for zero
0
0
0
0
0
0
0
.
1
0

echo sprintf("%'z10.2f", .1); // single quote needed for all except zero
z
z
z
z
z
z
0
.
1
0

$x = 10;
$y = -10;
echo sprintf("%+d", $x);
echo sprintf("%+d", $y);

printf

echo printf("%s", "XXXX"); // XXXX4, Returns the length of the outputted string

<?php
$intChar = 65;
printf('%c', $intChar); // A

Friday, 11 March 2016

Difference among intersect functions

1. array_intersect 
শুধু data চেক করে, index চেক করেনা


2. array_uintersect 
data চেক করে user defined function এর মধমে, index চেক করেনা

3. array_uintersect_assoc
data চেক করে user defined function এর মধমে, index চেক করে

4. array_intersect_uassoc
 data চেক করে, index চেক করে user defined function এর মধমে

5. array_uintersect_uassoc
data চেক করে user defined function এর মধমে, index চেক করে user defined function এর মধমে 

Saturday, 5 March 2016

Difference among Sort, SORT_NATURAL, SORT_FLAG_CASE

asort($array1,SORT_NATURAL) == natsort($array1) == uasort($array1, "strnatcmp");

asort($array1,SORT_NATURAL | SORT_FLAG_CASE) == natcasesort($array1) == uasort($array1, "strnatcasecmp");

$array1 = array("img12.png", "img10.png", "img2.png", "img1.png");

sort($array1);
echo "Standard sorting\n";
print_r($array1);

sort($array1,SORT_NATURAL);
echo "\nNatural order sorting\n";
print_r($array1);

$array1 = array("img12.png", "img10.png", "Img2.png", "img1.png");

sort($array1);
echo "Standard sorting\n";
print_r($array1);

sort($array1,SORT_NATURAL); // like natsort, but index not preserved.
echo "\nNatural order sorting\n";
print_r($array1);

sort($array1,SORT_NATURAL | SORT_FLAG_CASE); like natcasesort, but index not preserved.
echo "\nNatural order sorting, case insensitive\n";
print_r($array1);

Friday, 4 March 2016

Array Specials

<?php
echo "<pre>";
$array = array ('3' => 'a', 3 => 'b', 3.5=>"c", "3.5"=>"d", -3=>"e", -3.5=>"f", "-3"=>"g", "-3.5"=>"h");
print_r($array);
?>

Array
(
    [3] => c
    [3.5] => d
    [-3] => g
    [-3.5] => h
)
 
$array = array (true => 'a', 1 => 'b', "1" => 'c');
var_dump ($array); //c

$array = array (true => 'a', 1 => 'b');
var_dump ($aray); //wrong variable, NULL


$array = array ("" => 'a', null => 'b');
var_dump ($array); //b, NULL converted to emptry string