PHP – CodeIgniter how to create and use a model

MacBook avec code sur un bureau

Looking at the structure of CodeIgniter we see that there is a folder called application/models. It is in this one that we will create our model. For a model to be usable as such, it must extend CI_Model. class ScoresModel extends CI_Model { function __construct(){ // Call the Model constructor parent::__construct(); $this->load->database(); } function getAllScores(){ … Read more

PHP – Create a REST API with CodeIgniter

Code sur écran d'ordinateur

Introduction CodeIgniter is a fairly simple framework for PHP. Compared to its big brothers such as Zend or Yii, it has the advantage of being small and very simple to get to grips with. For me, currently the best for small projects such as creating a REST-API. Installation To start, go download the official version … Read more

WordPress – how to retrieve the ID from a given meta

Code sur écran d'ordinateur

With WordPress, it is common to use the get_post_meta($post, $meta_key) method in order to retrieve the value of a given meta. However for the ID, there is no ready-made method. The best solution I’ve found so far: $mid = $wpdb->get_var( $wpdb->prepare(“SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s”, $post, $meta_key) );

PHP – Protection of special characters of regular expressions

Code sur écran d'ordinateur

If you want to use a regular expression to, for example, search for a word in a paragraph, it is important to protect your variable. To do this, just use the following function: string preg_quote ( string $str [, string $delimiter = NULL ] ) For example: foreach ($aRequest[‘keywords’] as $sKeyword) { $sKeyword = preg_quote($sKeyword); … Read more

Categories PHP

JavaScript – Iframe – the address where the iframe is located

MacBook avec code sur un bureau

For security reasons, you can’t directly reach the page outside the iframe from inside it. However, we can retrieve information from the browser. To retrieve the parent’s address, i.e. the URL of the page where the iframe is added, just write this: <script type=”text/javascript”> var url = (window.location != window.parent.location) ? document.referrer : document.location; alert(url); … Read more

jQuery – Word and Character Counter

MacBook avec code sur un bureau

A small script to count the number of words and/or characters in an input or textarea field <input id=”text” name=”text” type=”text” /> <span id=”count”></span> We place the script in jQuery’s ready function to make sure the HTML has been fully loaded. $(document).ready(function() { count($(“#text”), $(“#count”)); $(“#text”).keyup(function() { count($(“#text”), $(“#count”)); }); }); function count(src, dest) { … Read more

PHP – mb_strlen: get the real length of a string

MacBook avec code sur un bureau

A lot of code uses strlen to get the size of a string. The problem: strlen returns the number of bytes, not the number of characters. As soon as a string contains accented letters or emojis, the result is wrong. The problem with strlen In UTF-8, a character such as “é” takes two bytes, so … Read more

Categories PHP

PHP – Replace accented characters with their unaccented equivalents

MacBook avec code sur un bureau

A small function to replace accented characters with their unaccented equivalent. function normalize_str($str) { $invalid = array(‘Š’=>’S’, ‘š’=>’s’, ‘Đ’=>’Dj’, ‘đ’=>’dj’, ‘Ž’=>’Z’, ‘ž’=>’z’, ‘Č’=>’C’, ‘č’=>’c’, ‘Ć’=>’C’, ‘ć’=>’c’, ‘À’=>’A’, ‘Á’=>’A’, ‘Â’=>’A’, ‘Ã’=>’A’, ‘Ä’=>’AE’, ‘Å’=>’A’, ‘Æ’=>’A’, ‘Ç’=>’C’, ‘È’=>’E’, ‘É’=>’E’, ‘Ê’=>’E’, ‘Ë’=>’E’, ‘Ì’=>’I’, ‘Í’=>’I’, ‘Î’=>’I’, ‘Ï’=>’I’, ‘Ñ’=>’N’, ‘Ò’=>’O’, ‘Ó’=>’O’, ‘Ô’=>’O’, ‘Õ’=>’O’, ‘Ö’=>’OE’, ‘Ø’=>’O’, ‘Ù’=>’U’, ‘Ú’=>’U’, ‘Û’=>’U’, ‘Ü’=>’UE’, ‘Ý’=>’Y’, ‘Þ’=>’B’, … Read more

Categories PHP