Skip to content

Instantly share code, notes, and snippets.

View vaibhavhrt's full-sized avatar
🙃
Focusing

Vaibhav Vishal vaibhavhrt

🙃
Focusing
View GitHub Profile
@vaibhavhrt
vaibhavhrt / get-html-from-any-webpage.php
Last active April 14, 2018 05:52
code to get text, href, etc data from any webpage using simple_html_dom in php
<?php
$pageURL = "http://www.google.com"; //replace with the url of page you wanna access
require("simple_html_dom.php"); //download simple_html_dom parser from here: "http://simplehtmldom.sourceforge.net/" and put it in the same location as this .php file
$html = file_get_html($pageURL);
foreach($html->find('a[class=xyz]') as $element){ //replace a[class=xyz] with the elemets you want to get, read here for more info: http://simplehtmldom.sourceforge.net/manual.htm
echo $element->href . '<br>'; //display the href just for testing purposes
} //$element is an object with all the attributes of all matching elements according to your selector, do whatever you want with it.
// if getting this error: Warning: file_get_contents(): stream does not support seeking in simple_html_dom.php on line 76,
//On line 76 of simple_html_dom.php:
function crypto_rand_secure($min, $max) {
$range = $max - $min;
if ($range < 0) return $min; // not so random...
$log = log($range, 2);
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
@vaibhavhrt
vaibhavhrt / most-frequent-word.js
Created April 21, 2018 07:17
Challenge: analyze a most frequent word program
function getTokens(rawString) {
// NB: `.filter(Boolean)` removes any falsy items from an array
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
//split() splits the paragraph into words using regular expression
}
function mostFrequentWord(text) {
var words = getTokens(text); //use getTokens to get an array of words from the paragraoh text
var wordFrequencies = {}; //defing an object to save frequency of every word
for (var i = 0; i <= words.length; i++) { //loop to iterate through the array of words
@vaibhavhrt
vaibhavhrt / select-one-row-from-mySql-db.php
Last active April 27, 2018 08:46
php function to query and get one row from a mysql database
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'database_name');
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($con === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
@vaibhavhrt
vaibhavhrt / dict-of-lists-to-json.py
Last active August 17, 2018 15:42
[Python] Get json string from a python dict containing one or more list of strings into json, all list must be of same size
""" Get json string from a python dict containing lists of same size, see example at bottom of code """
import pyperclip
def dict_to_json(lists, cont=False, start_from=0):
""" convert a list of strings to json """
if cont:
json = ''
else:
json = '{\n'
keys = []