www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Replacing Repeated Words

  1. #1
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063

    resolved [RESOLVED] Replacing Repeated Words

    I've been experimenting with preg_replace() today, but I can't figure out how to replace words that are repeated. I can the first word replaced, but the same word right after that word is still output to the browser.

    PHP Code:

    $string1 
    '/[^a-zA-Z]+(the)[^a-zA-Z]+/i';

    $string2 'ha';

    $string3 'A the the joke';

    echo 
    preg_replace($string1$string2$string3); 
    What I'm trying to do is replace a word that a) has one or more characters in front of it (except letters) followed by b) the substring "the" followed by c) one or more characters that are not letters. What am I doing wrong?

  2. #2
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    PHP Code:
    function curseNo($string)
      {
      
         
    // create an empty array for words
         
         
    $swear = array();
         
         
    // fill the array with word patterns
         
         
    $swear[] = '/[^a-zA-Z]+?(chuffing)[^a-zA-Z]??/i';
         
    $swear[] = '/(chuffing)[^a-zA-Z]??/i';
         
    $swear[] = '/[^a-zA-Z]+?(chuffer)[^a-zA-Z]??/i';
         
    $swear[] = '/^(chuffer)[^a-zA-Z]??/i';
         
    $swear[] = '/[^a-zA-Z]+?(chuff)[^a-zA-Z]??/i';
         
    $swear[] = '/^(chuff)[^a-zA-Z]??/i';
         
         
    // replace the words
         
         
    $string preg_replace($swear'!@#$%'$string);
         
         
    // return it
         
         
    return $string;
         
      } 
    Well, I got the last problem fixed, but now there's something I don't understand. In the sentence 'Chuff chuffing chuffer!', it's replaced by '!@#$%!@#$%' instead of '!@#$%!@#$%!@#$%'. What gives?

  3. #3
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    Hey, nothing to do with this post but Joseph Witchard - I love your signiture, stamp out those idiots =)

    Ryan

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    Lack of knowledge != idiocy. For example, you've posted here asking for help, does that make you an idiot?

  5. #5
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    Well yeah i guess your right on that one. Not like i thought about that =p

  6. #6
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    Well, anyway, I've got the problem worked out. Resolved.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles