SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Mr Jojo's Avatar
    Join Date
    May 2007
    Posts
    322
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple help with preg_replace and regex

    Hello.

    I have this string:

    $text = "banana pineapple fitness fat banana apple fat fatness";

    And i want to cut all the repeated words, wich are "banana" and "fat".

    How can i do it with regex and preg_replace or other php function?

    Result should be:

    $text = "banana pineapple fitness fat apple fatness";

    or

    $text = "pineapple fitness banana apple fat fatness";

    (the order of the words doesnt need to be the same after the replacing process)

    Thanks!

  2. #2
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you don't need regular expressions here:

    PHP Code:
    $arr explode(" "$text);
    $arr2 array_unique($arr);
    $text2 implode(" "$arr2); 
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  3. #3
    SitePoint Addict Mr Jojo's Avatar
    Join Date
    May 2007
    Posts
    322
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!


Bookmarks

Posting Permissions

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