PHP preg_replace all but letters and numbers
If you want to remove all characters from a string using ascii range, this is one setup you can use. To modify see the HEX ascii values in this ascii table.
If you want to remove all characters from a string using ascii range, this is one setup you can use. To modify see the HEX ascii values in this ascii table.
This function removes all HTML and keeps the plain text and is an enhancement of PHP strip_tags() function by strip out styles, scripts, embedded objects, and other unwanted page code.
This is how you can format a number to money format with thousands separator, delimiter and set precision, e.g. from 12345 to 12 345.00
This is how you can remove line breaks and carriage return from a string.
<?php
$string = trim( preg_replace( '/\s+/', ' ', $string ) );
?>
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;
?>
/**
* 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 ?
This is how you can extract a domain out of URL without the subdomains. The parse_url() function gives the domain with the subdomain and this function just take the last two pieces and return it put together.
This is how you can filter a text and load it into an array. This function converts all words into lower-case, removes all HTML tags, junk characters and numbers, explode it in to an array and finally removes duplicates.
This is how you can convert upper case characters/letters to lower case even if they contain swedish characters.
<?php
function mymodule_stringtolower($string) {
$search = array ('/Å/','/Ä/','/Ö/');
$replace = array ('å','ä','ö');
$string = preg_replace($search, $replace, $string);
$string = strtolower($string);
return $string;
}
?>
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;