strings

File name sanitizer

This is a way to sanitize a string to be used as file name or URL path etc. by removing unwanted characters or replacing them with a hyphen or similar.
It takes a string like:
Daily SimpliVity Backup 12/16/2018 02:00:215
and converts it into:
daily-simplivity-backup-12-16-2018-02-00-25

Remove empty places in comma separated list with preg_replace

This is how you can remove empty places in a comma separated list e.g when you have a list like "one, , two, , three, , " and you want it to look like "one, two, three"

<?php
    $input
= "one, , , two, ";
   
//$input = ", two, two, ";
    //$input = ", two, two";
   
   
$input = preg_replace( "/, , |, $|^, /", "", $input);

    echo
$input;
?>

A sort of teaser function splitting inbetween two words


/**
* Get the position in a text inbetween two complete words just below
* a limit if the text is longer than the limit.
*
* @param unknown_type $text
* @param unknown_type $limit
* @return unknown
*/
function _get_complete_word($text, $limit = 5000) {

$text_lenght = strlen($text);

if ($text_lenght <= $limit) {

return $text_lenght;

}else{

preg_match_all("/ /ui", $text, $matches, PREG_OFFSET_CAPTURE);

foreach ($matches[0] as $key => $value) {
if ($value[1] >= $limit) {
return ($key == 0 ?

How to split a text into chuncks containing a maximum number of characters and retain full sentences

This is how you can split a text into chuncks containing a maximum number of characters and retain full sentences.As an example, if you have a limit of 5000 characters, as in the case of Google Translate API, and would like to divide the text into chunks, but you do not want sentences or words to be divided in the middle.

Support Ticketing System module file name error fix

To fix Support Ticketing System module filename error I have used this hack in support.module. The problem is that if the file name contains other characters than A-Z it end up in some unconverted file name that is not good looking and can in some cases make it hard to open. The file name can look like this: "=?ISO-8859-1?Q?Spr=E5khantering=2Epdf?=" when it should be "Språkhantering.pdf". It seems like urldecode() encounters a problem and returns some sort of ISO string. My solution was to replace all non A-Z characters by "x".

How to filter strings and just allow a-z and A-Z and /or numbers

This is how you can filter strings and just allow a-z and A-Z and /or numbers.

<?php
//Allow only a-z, A-Z and 0-9
         
if(ctype_alnum($value) > 0) {
           
$tags_array[] = $value;
          }

//Allow only a-z and A-Z
         
if(ctype_alpha($value) > 0) {
           
$tags_array[] = $value;
          }
?>

Read more about ctype_alnum() or ctype_alpha()

Pages