Category Archives: Uncategorized

Regex Remove Blank Lines from Text in PHP

This regex will remove (preg_replace) all empty new lines from a text: $str = preg_replace(‘/^\n+|^[\t\s]*\n+/m’, ”, $str);$str = preg_replace(‘/^\n+|^[\t\s]*\n+/m’, ”, $str);

Javascript Get Window Location Hostname

Various example demonstration how to get URL on the address bar using Javascript, which include location.hostname, window.top.location, window.location.origin, and more. In the example below I included also an example to remove the www. from the website address. Here is the script code to get various locations with Javascript var x = location.hostname; document.getElementById("demo1").innerHTML= x; var […]

Best Google Fonts for Your Web Projects

My favorites Google Fonts for web projects are: Lato (10 Styles) Open Sans (10 Styles) Neuton (6 Styles) Montserrat (2 Styles) Source Sans Pro (12 Styles) Raleway (18 Styles) PT Sans (4 Styles) Droid Sans (2 Styles) Quicksand (4 Styles) Nunito Sans (14 Styles)

Java HashCode in PHP and JavaScript

JavaScript function of Java hashCode(): /** * @see http://stackoverflow.com/q/7616461/940217 * @return {number} */ String.prototype.hashCode = function(){ if (Array.prototype.reduce){ return this.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0); } var hash = 0; if (this.length === 0) return hash; for (var i = 0; i < this.length; i++) { var character = this.charCodeAt(i); hash = ((hash<<5)-hash)+character; hash = hash & hash; // […]

Example usage PHP Namespace and Use

Pay attention to the order, namespace goes at first line: namespace LayerShifter\TLDExtract\Tests;   include_once __DIR__ . ‘/vendor/autoload.php’;   use LayerShifter\TLDDatabase\Store; use LayerShifter\TLDExtract\Exceptions\RuntimeException; use LayerShifter\TLDExtract\Extract; use LayerShifter\TLDExtract\Result;   // Below here insert custom functions etc   $extract = new Extract();   $extract->setExtractionMode(Extract::MODE_ALLOW_ICCAN);   $result = $extract->parse(‘a.b.blogspot.com’);   print_r($result->getRegistrableDomain());namespace LayerShifter\TLDExtract\Tests; include_once __DIR__ . ‘/vendor/autoload.php’; use LayerShifter\TLDDatabase\Store; use […]

PHP Validate URL

PHP function that validates and URL: function is_website_url($url, $allowed_schemes = array(‘http’, ‘https’)) { $validate_url = !(filter_var($url, FILTER_VALIDATE_URL) === false); $scheme = parse_url($url, PHP_URL_SCHEME); return $validate_url && in_array($scheme, $allowed_schemes, true); }function is_website_url($url, $allowed_schemes = array(‘http’, ‘https’)) { $validate_url = !(filter_var($url, FILTER_VALIDATE_URL) === false); $scheme = parse_url($url, PHP_URL_SCHEME); return $validate_url && in_array($scheme, $allowed_schemes, true); } Usage: $url […]

PHP Curl Class

Awesome PHP curl class found on github: $ composer require php-curl-class/php-curl-class &> /dev/null $ php –interactive php > require __DIR__ . ‘/vendor/autoload.php’; php > use \Curl\Curl; php > $curl = new \Curl\Curl(); php > $curl->setBasicAuthentication(‘user’, ‘pass’); php > $curl->get(‘https://api.github.com/user’); php > echo $curl->httpStatusCode; 200 php > echo $curl->responseHeaders[‘content-type’]; application/json; charset=utf-8 php > echo $curl->response->login; php-curl-class […]

Example FFprobe commands

Extract index, codec name and codec type: ffprobe -v error -show_entries stream=index,codec_name,codec_type input.mkvffprobe -v error -show_entries stream=index,codec_name,codec_type input.mkv Example output: [STREAM] index=0 codec_name=h264 codec_type=video [/STREAM] [STREAM] index=1 codec_name=vorbis codec_type=audio [/STREAM] [STREAM] index=2 codec_name=vorbis codec_type=audio [/STREAM] [STREAM] index=3 codec_name=vorbis codec_type=audio [/STREAM] [STREAM] index=4 codec_name=ass codec_type=subtitle [/STREAM][STREAM] index=0 codec_name=h264 codec_type=video [/STREAM] [STREAM] index=1 codec_name=vorbis codec_type=audio [/STREAM] [STREAM] […]

Check if a website is located in China

To check if a website is located in China you can parse the NS DNS records: host => rbacc-us.com class => IN ttl => 86400 type => NS target => f1g1ns1.dnspod.net   host => rbacc-us.com class => IN ttl => 86400 type => NS target => f1g1ns2.dnspod.nethost => rbacc-us.com class => IN ttl => 86400 […]