Recent Topics

1 Apr 03, 2008 22:00    

I can probably cobble up something ugly, but I'm hoping it's already been done and someone can point me to an answer.

What I want to do is take an unknown amount of text and break it up into reasonably even-length lines without breaking words in half. So like be able to take $long_string, find "the next space after the 60th character", chop that bit off and save it as $long_string_bits[0] or $long_string_1 or whatever, then repeat until such times as $long_string is a bunch of short strings - without cutting a word in half just because the 60th character happened in the middle of a word.

Any pointers out there?

2 Apr 03, 2008 23:16

The feed reader thing I coded has a function in it for cutting content at word length ... it ignores stuff in tags, which is a bonus, but it discards the rest, whereas you kinda wanna keep that bit .... might be worth looking at as a potential starting point

The chances are that it doesn't even remotely do what you want to do, but you never know ;)

¥

4 Apr 04, 2008 00:17

yeah, but if I recall correctly that gets screwed by tags ;)

<word1 word2="word3:word4;word....urm, bugger I lost count ... 9?;">word6 word7 word8</word1..10>

¥

5 Apr 04, 2008 01:05

The total number of words doesn't help much given that the goal is reasonably similar line lengths without breaking words in half, but then again ... in theory one could explode(long_string) into bits and count(str_split(bit)) each bit and add the bits together while() the cumulative character count is less than the targetted line length. But then tags get busted in half so it would have to strip tags out ... then ...

But wait a minute I won't be having tags in this situation because what I want to do is take a textarea input then make it look like a nicely formatted commented out bit at the top of a plugin's php file. So all I care about is that plain old text is broken into reasonably appropriate lengths before a "line break" for the next line of a comment.

More importantly, why would I burn calories on such a trivial little detail when so far all MOAPGs can do is store info in a database and send an email saying "hooray I stored info in a database"?

Because details are cool?

6 Apr 04, 2008 11:08

How about summat like this :

<?php
$foo = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum volutpat dictum lectus. Phasellus et pede scelerisque tellus
 blandit consequat. Sed ultrices magna sit amet mi faucibus vulputate. Nam tincidunt aliquet quam. Mauris venenatis est sit amet turpis. Fusce
 tincidunt, metus nec egestas gravida, diam metus ultricies massa, a euismod odio metus a nunc. Cras mattis, urna at posuere condimentum,
 lorem turpis adipiscing nulla, eu venenatis urna tellus quis nibh. Praesent sit amet magna et metus volutpat convallis. Pellentesque habitant morbi
 tristique senectus et netus et malesuada fames ac turpis egestas. Cras malesuada pede non augue.';

$bar = preg_split( '~[\s]+?~', $foo );

$count = 0;
$max_length = 40;

$formatted = '';

foreach( $bar as $pint )
{
	$count += strlen( $pint ) + 1;
	$formatted .= ' '.$pint;
	if( $count > $max_length )
	{
		$count = 0;
		$formatted .= "\n";
	}
}

echo $formatted;
?>

¥

*edit*

Or even :

<?php
$formatted = preg_replace( '~(.{'.$max_length.'}([\S]+?)?[\s])~', '$1'."\n", $foo );
echo $formatted;
?>


?>

7 Apr 04, 2008 14:54

Okay wow cool. I'll play after I go make some money. I got mister program to actually start doing some actual work but for sure every detail counts yah?

I had this feeling when looking at ... (aw crap) a site I forgot to bookmark that word boundaries were somehow part of the answer, assuming that regex was the smarter part of the answer of course. But I can't even fake a regex.

This is turning out to be quite the project :) Seems having a program write programs isn't exactly the walk in the park I envisioned because of those stupid things called "details". Like, actually having it ... you know ... *work* is kinda like, well, work ;)

8 Apr 04, 2008 15:10

Hooray! I like the second one betterer because it's smaller. Plus guess what I just noticed. My stupid host moved me to a new server. Guess how I noticed? They forgot to do the daylight saving time thing :lol: So now I gotta tell everything the server is an hour ago from when it thinks it is relative to me so that one day when they figure it out I can set it back to what it is so that when daylight saving time happens again I can set them all up (or is it down) again. I thought living in Arizona would mean I could kiss that clock swapping crap goodbye, but noooooooo. Seems my servers like to live in the dark ages... I should buy a server and advertise hosting in AZ as always on the real time no matter what the rest of the country/world is doing with *their* clocks :D

10 Apr 04, 2008 17:02

cool, that seems to be doing the trick

11 Apr 04, 2008 17:10

Funny thing is I was looking for something completely unrelated when I found that function. Unfortunately it does nothing for the other mission...

That seems to be my luck with looking for groovy functions on php.net: they show other funcs related to whatever I'm looking at, and most of the time they don't help me with what I'm trying to do, but do manage to pop up something that would have helped with something else I already either cobbled through or gave up on.

No time to actually test wordwrap in comparison to the methods ¥åßßå crafted up though as I have the opportunity to do some overtime and, though my job sucks and I hate going there, making time and a half for a few hours sounds appealing.


Form is loading...