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 " - "
![]()
| SitePoint Sponsor |
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 " - "
![]()



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!
thanks tri4almaniac, but it doesnt seem to work
thanks for trying anyway,
mainman
any1?
bump



That worked for me, at least as I understood your question. Why not post some sample input and output?
It works for me. Post what doesn't work for you.
Location: Alicante (Spain)... Hot and Sunny...
Texas Holdem Poker Probability Calculator | DNS test
Avatars | English Spanish Translation | CAPTCHA with audio
Email | PHP scripts | Cruft free domain names | MD5 Cracker
im using what you gave me:
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$message = preg_replace('/\w{30}/', "$0-", $message);
Thanks





Does this:
match what you said here: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
You neither have a word longer than 30 characters, nor does it break the word after 30 characters.that can search a string $message for words longer than 30 charcters and if so break the word at 30



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:
As that causes the expression to refrain from adding a trailing hypen to the tail end of the word.PHP Code:$message = preg_replace('/\w{15}(?=\w)/', "$0 - ", $message);





Count again. Or, rather don't and wait a week until the poster makes up his mind.Well that's cutting the word after 15 characters



Curses, didn't bother to actually count the individual lettersOriginally Posted by 7stud
![]()
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.
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.



I think the regex is by far the simplest. At least to that degree of accuracy.
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.
ok thanks for the help guys, and sorry i explained it badly![]()



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.Originally Posted by Icheb
Learn regular expressions if you haven't already. They are your key to happiness![]()
I guess you didn't get the phrase "And for a beginner it's probably much easier to comprehend than regular expressions".Originally Posted by trib4lmaniac



We'll have to agree to differ I suppose.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.
As a side note, wordwrap might be what you're looking for.
Wordwrap doesn't work in this context.Originally Posted by exam
Location: Alicante (Spain)... Hot and Sunny...
Texas Holdem Poker Probability Calculator | DNS test
Avatars | English Spanish Translation | CAPTCHA with audio
Email | PHP scripts | Cruft free domain names | MD5 Cracker
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.Originally Posted by bokehman



Originally Posted by Icheb
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.PHP Code:function word_split($text, $divider, $length) {
$text = explode(" ", $text);
foreach($text as $key => $word) {
$word = chunk_split($word, $length, " - ");
$text[$key] = substr($word, 0, -strlen($divider));
}
$text = join($text, " ");
return $text;
}
$message = word_split($message, " - ", 15);
Compare to:
I think the regex is simpler and betterPHP Code:$message = preg_replace('/\w{15}(?=\w)/', "$0 - ", $message);
![]()
Bookmarks