string

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 ?

Check if string is UTF-8

Have not tested it...

function check_utf8($str) {
$len = strlen($str);
for($i = 0; $i < $len; $i++){
$c = ord($str[$i]);
if ($c > 128) {
if (($c > 247)) return false;
elseif ($c > 239) $bytes = 4;
elseif ($c > 223) $bytes = 3;
elseif ($c > 191) $bytes = 2;
else return false;
if (($i + $bytes) > $len) return false;
while ($bytes > 1) {
$i++;
$b = ord($str[$i]);
if ($b < 128 || $b > 191) return false;

Pages