GetDotted Domains

Viewing Thread:
"Php Homework"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Wed 03/11/04 at 17:03
Regular
"Lisan al-Gaib"
Posts: 7,093
Write a routine to split a paragraph into two roughly equal length paragraphs.

I.e.

"This is a test. This is also a test."

To : -

"This is a test."
"This is also a test."

Simply splitting the paragraph ala,

echo substr($para,0,strlen($para)/2);
echo substr($para,(strlen($para)/2)+1,strlen($para));

Is not acceptable. Anyone do this quickly? I desperately need it.
Sat 06/11/04 at 12:34
Regular
"It goes so quickly"
Posts: 4,083
Well, I really was off the ball then - focusing on the 'cutting in two' aspect and not looking at the real problem, sorry about that.

Nice example Garin, and your theory of finding the middle, then the closet full stop is spot on.
Thu 04/11/04 at 08:57
Regular
"Lisan al-Gaib"
Posts: 7,093
Garin wrote:

> I guess the problem is that it takes no account of where you're
> splitting, ie you might be splitting mid word.

Yup, exaclty. It's for a news page on a website.

> Presumably he wants something like:
>
> $half = floor(strlen($paragraph) / 2);
> $sec1 = strrpos(substr($paragraph,0,$half+1),".");
> $sec2 = strpos(substr($paragraph,$half+1),".");
> if (($half-$sec1)<(($half+$sec2)-$half)) {
> $para1=substr($paragraph,0,$sec1+1);
> $para2=trim(substr($paragraph,$sec1+1));
> } else {
> $para1=substr($paragraph,0,($half+$sec2+2));
> $para2=trim(substr($paragraph,($half+$sec2+2)));
> }
>
> Not tested that so maybe it works, maybe it doesn't. :P But the
> theory is, from the middle point of the paragraph, find the nearest
> full stop on either side, whichever is closest, split there. Depends
> on your criteria for splitting though.

Top stuff! Works perfectly! Top of the class. :)
Seriously though, cheers mate. That's saved me some time this morning.
Thu 04/11/04 at 00:05
Regular
"Devil in disguise"
Posts: 3,151
cjh wrote:
> I'd have done it like this:
>
> $half = $strlen($paragraph) / 2;
> $sec1 = substr($paragraph,0,$half);
> $sec2 = substr($paragraph,($half+1));
>
> Pretty similar though, so why was yours not acceptable? (Though I
> expect PHP has a function that is designed to do this)

I guess the problem is that it takes no account of where you're splitting, ie you might be splitting mid word.

Presumably he wants something like:

$half = floor(strlen($paragraph) / 2);
$sec1 = strrpos(substr($paragraph,0,$half+1),".");
$sec2 = strpos(substr($paragraph,$half+1),".");
if (($half-$sec1)<(($half+$sec2)-$half)) {
$para1=substr($paragraph,0,$sec1+1);
$para2=trim(substr($paragraph,$sec1+1));
} else {
$para1=substr($paragraph,0,($half+$sec2+2));
$para2=trim(substr($paragraph,($half+$sec2+2)));
}

Not tested that so maybe it works, maybe it doesn't. :P But the theory is, from the middle point of the paragraph, find the nearest full stop on either side, whichever is closest, split there. Depends on your criteria for splitting though.
Wed 03/11/04 at 22:20
Regular
"It goes so quickly"
Posts: 4,083
Pandaemonium wrote:
> echo substr($para,0,strlen($para)/2);
> echo substr($para,(strlen($para)/2)+1,strlen($para));
>
> Is not acceptable.


I'd have done it like this:

$half = $strlen($paragraph) / 2;
$sec1 = substr($paragraph,0,$half);
$sec2 = substr($paragraph,($half+1));

Pretty similar though, so why was yours not acceptable? (Though I expect PHP has a function that is designed to do this)
Wed 03/11/04 at 17:23
Regular
"Lisan al-Gaib"
Posts: 7,093
monkey_man wrote:
> My dog ate it.

Damn! Foiled!

Nah, I need it quickly, otherwise I'd write it myself! Did one aaaageeessss ago in C++, so it's possible (and pretty easy if I remember).
Wed 03/11/04 at 17:11
Regular
"Pouch Ape"
Posts: 14,499
My dog ate it.
Wed 03/11/04 at 17:03
Regular
"Lisan al-Gaib"
Posts: 7,093
Write a routine to split a paragraph into two roughly equal length paragraphs.

I.e.

"This is a test. This is also a test."

To : -

"This is a test."
"This is also a test."

Simply splitting the paragraph ala,

echo substr($para,0,strlen($para)/2);
echo substr($para,(strlen($para)/2)+1,strlen($para));

Is not acceptable. Anyone do this quickly? I desperately need it.

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Great services and friendly support
I have been a subscriber to your service for more than 9 yrs. I have got at least 12 other people to sign up to Freeola. This is due to the great services offered and the responsive friendly support.
Just a quick note to say thanks for a very good service ... in fact excellent service..
I am very happy with your customer service and speed and quality of my broadband connection .. keep up the good work . and a good new year to all of you at freeola.
Matthew Bradley

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.