Thread: Regex question

    #1
  1. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    I have a list of offensive words in a text file separated by the pipe symbol, like so:

    profanity1|profanity2|profanity3

    and I would like to write a PHP program to read those words into an array (I can do that bit, I think) then compares them to a string, and if that string contains any of those words, then the words are replaced with asterixes. How is this done?

    --------------------------------------------
    Alex
    (http://www.alex-greg.co.uk)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Washington, USA
    Posts
    52
    Rep Power
    14
    I'll assume you got down the part to get from the file and into the array. But you can use file("/path/to/file.txt") to slide the contents into an array and then explode by the pipe into an array nameed $profanity ... and then do something like what follows to actually test the string and replace case insensitively using eregi_replace.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $profanity = array("word1", "word2", "word3");
    /* test the word with a loop like this */
    $size = count($profanity);
    for($index = 0; $index < $size; $index++) {
    $string = eregi_replace($profanity[$index], "You talk to your mother with a mouth like that?", $string);
    }
    [/code]


    ------------------
    <UL TYPE=SQUARE>
    <LI> TD Scripts
    <LI> Script School
    <LI>php-scripts
    </UL>

Similar Threads

  1. Quick regex wildcard question.
    By s200 in forum PHP Development
    Replies: 2
    Last Post: February 12th, 2004, 09:09 AM
  2. REGEX Question
    By estrabd in forum Perl Programming
    Replies: 8
    Last Post: January 16th, 2004, 08:01 PM
  3. PHP data retrieval question with MySQL
    By gursharn in forum PHP Development
    Replies: 7
    Last Post: December 31st, 2003, 09:43 AM
  4. Javascript question mark question?
    By tajmiester in forum JavaScript Development
    Replies: 3
    Last Post: November 30th, 2003, 05:36 PM
  5. Radio Buttons
    By Ulysses Freeman in forum HTML Programming
    Replies: 4
    Last Post: November 15th, 2003, 09:57 PM

IMN logo majestic logo threadwatch logo seochat tools logo