Last active
May 1, 2021 15:25
-
-
Save dadinugroho/7a833c21b8a5cf043861f0ab03881d69 to your computer and use it in GitHub Desktop.
PHP weighted link rotators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* File: weighted-link-rotator.php | |
* | |
* This is a simple implementation of weighted link rotator. PHP weighted link rotator is a code to display one link chosen randomly | |
* from a list of URLs. However each URL has a weighted probability. A URL may have a bigger chance to be displayed when it has bigger | |
* probability. | |
*/ | |
/** | |
* Get a link with calculating its weighted probability. | |
* | |
* @param array $links array of URLs, 2D array in with the child array has url and weight as the key. | |
* | |
* @return string|bool the chosen url or return false if $links is empty array or no URL is found. | |
*/ | |
function get_link_to_display( $links ) { | |
if( empty( $links ) ) { | |
return false; | |
} | |
// Generate a random number between 1 to 100. | |
$rand_num = mt_rand( 1, 100 ); | |
// Filter only elements that has weight above the $rand_num | |
$filtered_arr = array_filter( $links, function( $val ) use ( $rand_num ) { | |
return ( $rand_num < $val['weight'] ); | |
}); | |
// Get the chosen one that is the first element of the filtered array. | |
$chosen = current( $filtered_arr ); | |
if (empty( $chosen ) or ! isset( $chosen['url'] ) ) { | |
return false; | |
} | |
// Show the URL. | |
return urldecode( $chosen['url'] ); | |
} | |
// Sample data. | |
$links = array(); | |
$links[] = array( | |
'id' => 'linkA', | |
'url' => 'https://www.google.com/imgres?imgurl=https%3A%2F%2Fimage.shutterstock.com%2Fimage-photo%2Fmountains-under-mist-morning-amazing-260nw-1725825019.jpg&imgrefurl=https%3A%2F%2Fstocksnap.io%2Fsearch%2Fnature&tbnid=ez-ubljHwN9MSM&vet=12ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygBegUIARDTAQ..i&docid=JEwZUI4DSxP5DM&w=390&h=280&q=images&safe=strict&ved=2ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygBegUIARDTAQ', | |
'weight' => 20, | |
); | |
$links[] = array( | |
'id' => 'linkB', | |
'url' => 'https://www.google.com/imgres?imgurl=https%3A%2F%2Fcdn.pixabay.com%2Fphoto%2F2015%2F04%2F23%2F22%2F00%2Ftree-736885__480.jpg&imgrefurl=https%3A%2F%2Fpixabay.com%2Fimages%2Fsearch%2Fnature%2F&tbnid=GEQYC6WSjtkqBM&vet=12ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygDegUIARDXAQ..i&docid=Ba_eiczVaD9-zM&w=771&h=480&q=images&safe=strict&ved=2ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygDegUIARDXAQ', | |
'weight' => 40, | |
); | |
$links[] = array( | |
'id' => 'linkC', | |
'url' => 'https://www.google.com/imgres?imgurl=https%3A%2F%2Fimages.ctfassets.net%2Fhrltx12pl8hq%2F4plHDVeTkWuFMihxQnzBSb%2Faea2f06d675c3d710d095306e377382f%2Fshutterstock_554314555_copy.jpg&imgrefurl=https%3A%2F%2Fwww.shutterstock.com%2Fdiscover%2F10-free-stock-images&tbnid=1Y5Fex7Bw6VMkM&vet=12ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygIegUIARDiAQ..i&docid=YAMnwpTKFlPEWM&w=2500&h=1227&q=images&safe=strict&ved=2ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygIegUIARDiAQ', | |
'weight' => 60, | |
); | |
$links[] = array( | |
'id' => 'linkD', | |
'url' => 'https://www.google.com/imgres?imgurl=https%3A%2F%2Fmedia.istockphoto.com%2Fphotos%2Fchild-hands-formig-heart-shape-picture-id951945718%3Fk%3D6%26m%3D951945718%26s%3D612x612%26w%3D0%26h%3Dih-N7RytxrTfhDyvyTQCA5q5xKoJToKSYgdsJ_mHrv0%3D&imgrefurl=https%3A%2F%2Fwww.istockphoto.com%2Fphotos%2Fpeace-maker&tbnid=1E4Z16ujrn_JRM&vet=12ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygGegUIARDdAQ..i&docid=27q4HQp-2Qmx0M&w=612&h=346&q=images&safe=strict&ved=2ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygGegUIARDdAQ', | |
'weight' => 80, | |
); | |
$links[] = array( | |
'id' => 'linkE', | |
'url' => 'https://www.google.com/imgres?imgurl=https%3A%2F%2Fcdn.pixabay.com%2Fphoto%2F2019%2F02%2F15%2F11%2F04%2Fbook-3998252__340.jpg&imgrefurl=https%3A%2F%2Fpixabay.com%2Fimages%2Fsearch%2Fweekend%2F&tbnid=hS9EDD38WRmkxM&vet=12ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygNegUIARDsAQ..i&docid=X9Ts63vD-KMxXM&w=510&h=340&q=images&safe=strict&ved=2ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygNegUIARDsAQ', | |
'weight' => 90, | |
); | |
$links[] = array( | |
'id' => 'linkF', | |
'url' => 'https://www.google.com/imgres?imgurl=https%3A%2F%2Fstatic.toiimg.com%2Fphoto%2F72975551.cms&imgrefurl=https%3A%2F%2Ftimesofindia.indiatimes.com%2Flife-style%2Fevents%2Fsurya-grahan-today-annular-solar-eclipse-2019-december-images-photos-pics-video-check-out-these-breathing-pictures-of-the-seasons-last-surya-grahan-of-26-december-2019-india%2Fphotostory%2F72975550.cms&tbnid=pTd2OIa33-6LVM&vet=12ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygRegUIARD0AQ..i&docid=78aIM0GrmvG9rM&w=1200&h=900&q=images&safe=strict&ved=2ahUKEwiqwp3OyqjwAhVRs0sFHUljCBgQMygRegUIARD0AQ', | |
'weight' => 100, | |
); | |
// The displayed link. | |
echo get_link_to_display( $links ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment