PHP - Explode function substitution - script with more delimiters of words at once
Separation of words from sentence; checking, correcting and censoring of isolated words; reconstruction of sentence.
2012-03-23
//Splitting of text string to an array elements (values) - substitution of explode function - for more delimiters at once
//Martin Adamek, www.adamek.cz
/////////////////////////////////////
//$delimiters Array of one-char delimiters (Two-characters delimiters are defined below, directly inside a code)
//$what The text string, which do you want to cut to single words
// Word-by-word corrected words will be sticked back to this variable again, in the end of a script
//$arrayofwordstocheck Array, where will be single isolated words saved to
/////////////////////////////////////
$delimiters=array(" ", ".", ",", ";", "-", "?", "!", "_", ":", "(", ")", "/");
$word=""; $arrayofwordstocheck=array();
// Cutting (splitting) of the string (sentence) to a words
for ($i=0; $i<strlen($what);$i++)
{
if (substr($what,$i,2)=="\\\"" or substr($what,$i,2)=="\\\\" or substr($what,$i,2)=="\\'")
{$arrayofwordstocheck[]=$word; $arrayofwordstocheck[]=substr($what,$i,2); $word="";$i++;}
//disclosing (finding) of characters escaped by the backslash - backslash as well as the both types of a quotation marks
elseif (in_array($what[$i], $delimiters))
{$arrayofwordstocheck[]=$word; $arrayofwordstocheck[]=$what[$i]; $word="";}
else
{$word.=$what[$i];}
}
$arrayofwordstocheck[]=$word;
unset($word);
/*
The whole splitted string is saved in the array $arrayofwordstocheck at this place,
you can ignore the rest of code for some applications and place the above-mentioned code into a function.
*/
// Checking, editting, correcting or another censorship of separated words
foreach ($arrayofwordstocheck as &$item)
{ if ($item=="YouCanNotFindMe") $item="IHaveFoundYou";
/* You can do what do you need with each isolated word. */
}
unset($prvek);
// Merging of corrected words into one string again (censored sentence)
$what=implode("",$arrayofwordstocheck);
//Because given delimiters are saved as single words (values of array elements), we can use a common implode function for re-merging of string
//- we can set empty string as a glue (space character usualy uses to be a glue, after using it as delimiter in explode function).
/*
We have corrected sentence as one text string in $what variable.
*/
So we have text-string completed in one string variable again, but we have editted wanted words in the way how we needed.
We could compare them with some list of expletives and set a star characters instead of them.
So you can use this algorithm e.g. as a base for script for filtering of vulgarisms (swear/dirty words) at discussions and comments.


unique content of this page
Navigation menu
This website is regularly archived by the National Library of the Czech Republic for its cultural, educational, scientific, research or other values with the aim of documenting an authentic sample of the Czech web. It belongs to a collection of Czech websites to be preserved by the National Library and made available for posterity. Its record is included in the Czech National Bibliography and the National Library catalogue.
Hide tools