Skip to content

Instantly share code, notes, and snippets.

View abdullahpazarbasi's full-sized avatar

Abdullah Pazarbaşı abdullahpazarbasi

View GitHub Profile
@abdullahpazarbasi
abdullahpazarbasi / email_regex_test.php
Created August 25, 2021 11:29
E-mail Regular Expression (Regex) Pattern PCRE
<?php
$sPattern = '^[^<>()[\]\\.,;:\s@"]+(?:\.[^<>()[\]\\.,;:\s@"]+)*@(?:xn--)?[a-zA-Z][a-zA-Z\d]*(?:-[a-zA-Z\d]+)*\.(?!local)[a-zA-Z]{2,32}(?:\.[a-zA-Z]{2})?$';
//
$sSubject = 'john.doe@example.org';
//
$aMatches = [];
if (preg_match($sPattern, $sSubject, $aMatches)) {
echo 'ok';
}
else {
@abdullahpazarbasi
abdullahpazarbasi / iso_date_time_format_regex.php
Created July 16, 2020 13:10
ISO Date-Time Format Regular Expression
<?php
$sPattern = '/^((((19|[2-9]\d)\d{2})\-(0[13578]|1[02])\-(0[1-9]|[12]\d|3[01]))|(((19|[2-9]\d)\d{2})\-(0[13456789]|1[012])\-(0[1-9]|[12]\d|30))|(((19|[2-9]\d)\d{2})\-02\-(0[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\-02\-29)) ([01]\d|2[0-3]):([0-4]\d|5[0-9]):([0-4]\d|5[0-9]) [+-](0\d|1[0-2]):(00|30)$/';
//
$sSubject = '2020-02-29 23:59:59 +03:00';
//
if (!preg_match($sPattern, $sSubject)) {
echo 'oops!';
}
@abdullahpazarbasi
abdullahpazarbasi / InterfaceHandler.php
Created January 27, 2020 07:36
JMS Serializer Interface Handler
<?php
namespace App\Infrastructure\Serializer\Handlers;
use JMS\Serializer\Exception\NotAcceptableException;
use JMS\Serializer\Exception\ObjectConstructionException;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use function get_class;
use function gettype;
@abdullahpazarbasi
abdullahpazarbasi / turkish_phone_number_regex_test.php
Last active January 11, 2023 15:21
Türkiye Telefon Numarası Regular Expression (Regex) Pattern PCRE
<?php
$sPattern = '/^(?:(?:\+90|0090|0)[ ]?)?(?:(?<ac>21[26]|22[2468]|23[26]|24[268]|25[268]|26[246]|27[246]|28[2468]|31[28]|32[2468]|33[28]|34[2468]|35[2468]|36[2468]|37[02468]|38[02468]|392|41[246]|42[2468]|43[2468]|44[26]|45[2468]|46[246]47[2468]|48[2468]|50[1567]|51[02]|52[27]|5[34]\d|55[1234569]|56[124]|59[246]|800|811|822|850|8[89]8|900)|\((?P<ac>\g<ac>)\))[ -]*(?<sn1>\d{3})[ -]*(?<sn2>\d{2})[ -]*(?<sn3>\d{2})$/J';
//
//$sSubject = '5554443322';
$sSubject = '(555)-444-33-22';
//
$aMatches = [];
if (preg_match($sPattern, $sSubject, $aMatches)) {
echo '(' . $aMatches['ac'] . ') ' . $aMatches['sn1'] . ' ' . $aMatches['sn2'] . ' ' . $aMatches['sn3']; // (555) 444 33 22
}
@abdullahpazarbasi
abdullahpazarbasi / xml_tag_regex.php
Created January 30, 2017 15:32
XML tag regular expression
$re = '/(<\s*(\w+)((?:\s+\w+(?:\s*=\s*(?:(?:"(?<=")(?:(?<=\\\\)"|[^"])*(?=")")|(?:\'(?<=\')(?:(?<=\\\\)\'|[^\'])*(?=\')\')))?)*)\s*>((?<=\>)(?:[^<>])*(?=\<))((?1))*((?<=\>)(?:[^<>])*(?=\<))<\s*\/\2\s*>)/i';
$str = '<a class="foo" style="bar: \'a\'; baz: \\"bax\\""> front
<b class="i"> inner <d attr= \'bak\\\'\\\'\'></d></b>
last
</a>';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);