Activity Stream
48,167 MEMBERS
6783 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 2 12 LastLast
Results 1 to 10 of 18
  1.     
    #1
    Google Corp.

    Default Preg replace help

    I need a php function including a preg replace line (or two) which will:
    - delete all brackets, apostrophe's, special characters
    - replace numbers with spaces
    - delete all words less than 3 characters
    - trim the spaces

    Unfortunately I'm a noob in php, and an uber-noob with regex.

    Is there anyone here who can help me out?
    SJshah Reviewed by SJshah on . Preg replace help I need a php function including a preg replace line (or two) which will: - delete all brackets, apostrophe's, special characters - replace numbers with spaces - delete all words less than 3 characters - trim the spaces Unfortunately I'm a noob in php, and an uber-noob with regex. Is there anyone here who can help me out? Rating: 5
    Life asked Death: "Why do people love me, but hate you?"
    Death responded: "Because you're a beautiful lie and I'm the painful truth."


  2.   Sponsored Links

  3.     
    #2
    Member
    lol good luck brother

    try to read some php ebooks

    i m reading 500 pages one
    Not Allowed

  4.     
    #3
    Respected Developer
    PHP Code: 
    <?php

    function clean($txt)
    {
        
    // remove everything that is not a word char, whitespace or a number
        
    $result preg_replace('/[^\w\d\s]/'''$txt);
        
    // replace numbers and trim spaces
        
    $result preg_replace('/(\d+|[ ]{2,})/'' '$result);
        
    // remove words <= 3 chars
        
    $result preg_replace('/\b[\w]{1,3}\b/'''$result);
        
        return 
    $result;
    }

    ?>
    Try that. Not sure if it works, never used regex in PHP.

  5.     
    #4
    Google Corp.
    Thanks Hyperz, its almost the way I want it

    Could you make it keep all hyphens "-" in tact and also trim any extra spaces?
    Life asked Death: "Why do people love me, but hate you?"
    Death responded: "Because you're a beautiful lie and I'm the painful truth."


  6.     
    #5
    Respected Developer
    By spaces do you mean all whitespace (incl. newlines etc)?

  7.     
    #6
    Google Corp.
    I mean spaces " ", like double/triple spaces and extra spaces on the ends of the string.
    There are no new lines.
    Life asked Death: "Why do people love me, but hate you?"
    Death responded: "Because you're a beautiful lie and I'm the painful truth."


  8.     
    #7
    Respected Developer
    Website's:
    wrzc.org
    Quote Originally Posted by SJshah View Post
    I mean spaces " ", like double/triple spaces and extra spaces on the end.
    There are no new lines.
    Doesn't that mean you'll end up with one long line of string like:

    Doesntthatmeanyoullendwithlonglinestringlike
    Tutorial How to SEO your Warez Site a guide to help you increase your organic traffic

    Huge list of Warez Sites and free Multiposter Templates

  9.     
    #8
    Google Corp.
    I mean any extra spaces, so if there is a double/triple space there it will be converted into a single space. Since the regex deletes all the numbers and words less than 3 chars, it leaves loads of extra spaces in once the string is returned.

    I already tried
    PHP Code: 
        $result trim($result" "); 
    but it only removes the extra spaces on the ends
    Life asked Death: "Why do people love me, but hate you?"
    Death responded: "Because you're a beautiful lie and I'm the painful truth."


  10.     
    #9
    Respected Developer
    PHP Code: 
    <?php

    function clean($txt)
    {
        
    // remove everything that is not a word, _, -, whitespace or a number
        // remove words <= 3 chars
        
    $result preg_replace('/([^\w\d\s\-]+|\b\w{1,3}\b)/'''$txt);
        
    // replace numbers with a space
        
    $result preg_replace('/\d+/'' '$result);
        
    // trim spaces
        
    $result preg_replace('/[ ]{2,}/'' '$result);
        
    // trim spaces at the end or beginning of lines
        
    $result preg_replace('/[ ]*(\r{0,1}\n)[ ]*/''$1'$result);
        
        return 
    $result;
    }

    ?>
    Had a little mistake in the other one too. Let me know if this does the trick.

    Edit:
    Added trimming of EOL spaces.

  11.     
    #10
    Google Corp.
    ^ that fixes the spaces prob

    but now it doesn't delete the words less than 3 chars from the string :/


    edit: looks like you accidentally edited out this line:
    PHP Code: 
        // remove words <= 3 chars
        
    $result preg_replace('/\b[\w]{1,3}\b/'''$result); 
    added it back in, looks like it works perfectly

    thanks m8
    Life asked Death: "Why do people love me, but hate you?"
    Death responded: "Because you're a beautiful lie and I'm the painful truth."


Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Preg and Sql
    By Chris2k in forum Web Development Area
    Replies: 6
    Last Post: 28th Apr 2012, 02:19 PM
  2. Which is the best replace tools
    By tcnnvnn in forum Webmaster Discussion
    Replies: 5
    Last Post: 29th Sep 2011, 09:40 AM
  3. Preg Replace question
    By Porsche_maniak in forum Web Development Area
    Replies: 7
    Last Post: 7th Nov 2010, 08:58 AM
  4. how to replace word in vb4
    By m107 in forum vBulletin
    Replies: 13
    Last Post: 4th Sep 2010, 09:20 AM
  5. how to replace bad words?
    By wman in forum vBulletin
    Replies: 2
    Last Post: 19th Feb 2009, 12:18 PM

Tags for this Thread

BE SOCIAL