Shortcut to the main page: Alt + Shift + upper 2(@)
Linkedin FB e-mail Google Plus Twitter

Search in this website

 
 
Language versions are not identical,
so visit all versions,
which you can understand.

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.

 

Made
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.

Jump up to: Navigation menu
(keyboard shortcut Alt + Shift + „5”)

Are you interested by this page?

  • Add to bookmarks (Ctrl+D)
  • Share link (CB radio)Jump up to:
  • Print (Ctrl+P)
  • Cite according to ISO 690

    This page

    ADÁMEK, Martin. 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.. Martin Adámek [online]. Náchod / Meziměstí (Czech Republic) [cited 2024-11-21]. Available from: https://www.adamek.cz/en/sw/php/explode-more-delimiters

    Whole site

    ADÁMEK, Martin. Martin Adámek [online]. Náchod / Meziměstí (Czech Republic) [cited 2024-11-21]. Available from: https://www.adamek.cz/en

 

 
 

National Cultural Heritage

WebArchiv – This website is being archived by the National Library of the Czech Republic 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.  

 

 
 

For a distraction

Why do all Assembler programmers want to live in Atlantis?
Because it is below C level.

 

For a muse

They laugh at me because I'm different; I laugh at them because they're all the same.
[Kurt Cobain]