SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Regexp to find words longer than 'X' in a given string

    Does anyone know if it is possible to build a regexp that can search a string $message for words longer than 30 charcters and if so break the word at 30 with a " - "


  2. #2
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string preg_replace('/\w{30}/'"$0-"$string); 
    Last edited by trib4lmaniac; Apr 22, 2006 at 07:29. Reason: Woah, forgot my php tags!

  3. #3
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks tri4almaniac, but it doesnt seem to work

    thanks for trying anyway,
    mainman

  4. #4
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any1?

  5. #5
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bump

  6. #6
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked for me, at least as I understood your question. Why not post some sample input and output?

  7. #7
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works for me. Post what doesn't work for you.

  8. #8
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im using what you gave me:

    $message = preg_replace('/\w{30}/', "$0-", $message);
    and say a user sends a message to another user, with a word like aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa which would break the table its displayed in, i want to cut that to aaaaaaaaaaaa - aaaaaaaaaaaa

    Thanks

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this:
    say a user sends a message to another user, with a word like aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa which would break the table its displayed in, i want to cut that to aaaaaaaaaaaa - aaaaaaaaaaaa
    match what you said here:
    that can search a string $message for words longer than 30 charcters and if so break the word at 30
    You neither have a word longer than 30 characters, nor does it break the word after 30 characters.

  10. #10
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that's cutting the word after 15 characters; not 30 as your first post stated. Just change the 30 to a 15 in the original snippet (now with spaces). Although on reflection I'd actually recommend using:
    PHP Code:
    $message preg_replace('/\w{15}(?=\w)/'"$0 - "$message); 
    As that causes the expression to refrain from adding a trailing hypen to the tail end of the word.

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that's cutting the word after 15 characters
    Count again. Or, rather don't and wait a week until the poster makes up his mind.

  12. #12
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    Count again. Or, rather don't and wait a week until the poster makes up his mind.
    Curses, didn't bother to actually count the individual letters

    If you want to split the words a little more intelligently you're going to need more than a single regular expression.
    I think the one I just posted will suffice for stopping the tables expanding waistband.

  13. #13
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternatively use explode() to put the string into an array and walk through the array to find words that are longer with strlen(). Would probably be easier than with a regular expression.

  14. #14
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the regex is by far the simplest. At least to that degree of accuracy.

  15. #15
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is that more accurate? Unless strlen() stops working both provide equally accurate results. And for a beginner it's probably much easier to comprehend than regular expressions.

  16. #16
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks for the help guys, and sorry i explained it badly

  17. #17
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    How is that more accurate? Unless strlen() stops working both provide equally accurate results. And for a beginner it's probably much easier to comprehend than regular expressions.
    I meant more simplistic for that degree of accuracy. If you write functionally equivalent code with string functions and make it look even simpler I will be very suprised.
    Learn regular expressions if you haven't already. They are your key to happiness

  18. #18
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac
    I meant more simplistic for that degree of accuracy. If you write functionally equivalent code with string functions and make it look even simpler I will be very suprised.
    I guess you didn't get the phrase "And for a beginner it's probably much easier to comprehend than regular expressions".

  19. #19
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    I guess you didn't get the phrase "And for a beginner it's probably much easier to comprehend than regular expressions".
    We'll have to agree to differ I suppose.

  20. #20
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you really want to tell me that some construction with strlen() etc. is more difficult to understand to a BEGINNER than a regular expression? You are absolutely hilarious.

  21. #21
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As a side note, wordwrap might be what you're looking for.

  22. #22
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by exam
    As a side note, wordwrap might be what you're looking for.
    Wordwrap doesn't work in this context.

  23. #23
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    Wordwrap doesn't work in this context.
    In retrospect, I have to agree with you. What I meant to say was that maybe instead of what the OP was trying to do, he could do with wordwrap, which does allow you to split long words. But after reconsidering, I don't think it'd be the best option.

  24. #24
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    So you really want to tell me that some construction with strlen() etc. is more difficult to understand to a BEGINNER than a regular expression? You are absolutely hilarious.
    PHP Code:
    function word_split($text$divider$length) {
        
    $text explode(" "$text);
        foreach(
    $text as $key => $word) {
            
    $word chunk_split($word$length" - ");
            
    $text[$key] = substr($word0, -strlen($divider));
        }
        
    $text join($text" ");
        return 
    $text;
    }

    $message word_split($message" - "15); 
    That does it very similarly, except it only splits at a single spaces. I'm not sure how it would play with things like tabs, multiple spaces and punctuation. Probably not nicely.

    Compare to:
    PHP Code:
    $message preg_replace('/\w{15}(?=\w)/'"$0 - "$message); 
    I think the regex is simpler and better


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
  •