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 some ways of removing the Swedish characters åäö in a string.
$search = array ('/'.chr(128).'/',
'/'.chr(157).'/',
'/'.chr(160).'/');
$replace = array ('', '', ' ');
$node_body = preg_replace($search, $replace, $node_body);
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.
Regular Expression | Will match... |
foo | The string "foo" |
^foo | "foo" at the start of a string |
foo$ | "foo" at the end of a string |
This is how you can escape slashes in an URL with an backslash, before doing a preg_replace on the path part of the URL. I have used this before a validation of URL:s containing the swedish characters åäö.
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;
}
?>
This is how you find a whole word by its boundary when the word is starting with åäö. The usual boundary expression can handle if Swedish characters is in the string but not if it starts (or ends) the word. That means that a word like "öknen" can't be found but a word like "behöver" will be found.
<?php
$text = "I öknen behöver man vatten";
$keyword = "öknen";
//$keyword = "behöver";
?>
This is the usual boundary expression
This is how you can extract words from a text, clean up a bit and store it in an array. The clean up function removes all characters that do not match; a to Z or A to Z or 0 to 9 or - (hyphen).