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

WordPress – How to run a LIKE query with WP_Query

MacBook avec code sur un bureau

To make queries on WordPress, I’m used to using WP_Query($args). In this example, we want to add a LIKE = “%title%” in our query. Here is a simple request that adds all our posts in the array a_response_array: $args = array( ‘post_status’ => ‘publish’, ); // The Query $query = new WP_Query( $args ); // … Read more

WordPress – Character counter for the caption field of an image

MacBook avec code sur un bureau

For every WordPress image, we can add a caption or a title. It may be useful to add a counter. To do this, you must use both hooks admin_head-post.php and admin_head-post-new.php function cf_caption_count_js() { echo ‘<script type=”text/javascript”> jQuery(document).ready(function() { jQuery(“#attachment_caption”).wrap(“<div></div>”); jQuery(“#attachment_caption”).parent().css(“display”, “table”); jQuery(“#attachment_caption”).parent().css(“width”, “100%”); jQuery(“#attachment_caption”).after(“<span id=\’cf_caption_count\’ style=\’display:table-cell; width: 30px; margin-left: 5px; text-align: right;\’></span>”); jQuery(“#cf_caption_count”).html(jQuery(“#attachment_caption”).val().length); jQuery(“#attachment_caption”).keyup(function() … Read more