Menu
I was wondering how I would go about splitting a string after a certain number of whatever…. Example. I want to split a string after the 50th {space}…
such as… This is a paragraph that should be split into two pieces.
How do I have that paragraph split after the word that which is the 5th space?
[code=php]
echo wordwrap($string, 20, "<br>n");
[/code]
[code=php]
<?php
$str = "The Philadelphia Wings finished the 2006 regular season with an 8-8 record, but missed the playoffs on a three-way tiebreaker. The Wings continue to build, and return an impressive lineup for 2007. Jake Bergey, Sean Greenhalgh and Jeff Ratcliffe are back on offense, while Tom Hajek, Peter Jacobs and Rob Van Beek are the top protected players on defense, with Matt Roik in goal.";
$five = explode(' ', $str);
$text = $five[0] ." ". $five[1] ." ". $five[2] ." ". $five[3] ." ". $five[4];
echo($text);
?>[/code]
[code=php]<?php
$text = 'The Philadelphia Wings finished the 2006 regular season with an 8-8 record, but missed the playoffs on a three-way tiebreaker. The Wings continue to build, and return an impressive lineup for 2007. Jake Bergey, Sean Greenhalgh and Jeff Ratcliffe are back on offense, while Tom Hajek, Peter Jacobs and Rob Van Beek are the top protected players on defense, with Matt Roik in goal.';
# number of words to capture
$capture = 5;
if(preg_match_all('/[A-Z][^.]+[.]/', $text, $matches))
{
foreach($matches[0] as $k => $match)
{
$first_five = implode(' ', array_slice($words = explode(' ', $match), 0, $capture));
$remainder = implode(' ', array_slice($words, $capture));
$sentences[] = array('first_five' => $first_five, 'remainder' => $remainder);
}
# test it
header('Content-Type: text/plain');
print_r($sentences);
}
else
{
echo 'No sentences found';
/* output produced
Array
(
[0] => Array
(
[first_five] => The Philadelphia Wings finished the
[remainder] => 2006 regular season with an 8-8 record, but missed the playoffs on a three-way tiebreaker.
)
[1] => Array
(
[first_five] => The Wings continue to build,
[remainder] => and return an impressive lineup for 2007.
)
[2] => Array
(
[first_five] => Jake Bergey, Sean Greenhalgh and
[remainder] => Jeff Ratcliffe are back on offense, while Tom Hajek, Peter Jacobs and Rob Van Beek are the top protected players on defense, with Matt Roik in goal.
)
)
*/
?>[/code]
[code=php]<?php
$paragraph = "The Philadelphia Wings finished the 2006 regular season with an 8-8 record, but missed the playoffs on a three-way tiebreaker. The Wings continue to build, and return an impressive lineup for 2007. Jake Bergey, Sean Greenhalgh and Jeff Ratcliffe are back on offense, while Tom Hajek, Peter Jacobs and Rob Van Beek are the top protected players on defense, with Matt Roik in goal.";
$sentences = explode(". ", $paragraph);
foreach($sentences as $value) {
//Display Sentences
echo ("$value<br />");
//Get the words or each sentence
$words_per_sentence = explode(" ", $value);
foreach($words_per_sentence as $key => $word) {
echo ("Word $key is $word<br />");
//Grab first five
if ($key <= 4) {
$first_five .= "$word ";
}
}
//Display first five of each sentence
echo ("$first_five<br />");
//Reset first five for next sentence
$first_five = "";
}
?>[/code]
[code=php]
//Grab first five
if ($key <= 4) {
$first_five .= "$word ";
} else {
//Else grab remaining
$remaining .= "$word ";
}
[/code]
[code=php]
/*
array first_words(str text, int numWords)
returns array: element 0 is first numWords of text, element 1 is rest of text
*/
function first_words($text, $numWords)
{
if(preg_match_all('/^((S+s+){1,'.$numWords.'})(.*)$/', $text, $matches) !== FALSE)
{
$output[0] = $matches[1][0];
$output[1] = $matches[3][0];
return($output);
}
else
{
return(FALSE);
}
}
// SAMPLE USAGE:
$test = <<<EOD
This is a test. It is only a test. The end.
EOD;
list($start, $end) = first_words($test, 5);
echo "START: $start'<br>";
echo "END: '$end'";
[/code]
[code=php]<?php
$text = 'The Philadelphia Wings finished the 2006 regular season with an 8-8 record, but missed the playoffs on a three-way tiebreaker. The Wings continue to build, and return an impressive lineup for 2007. Jake Bergey, Sean Greenhalgh and Jeff Ratcliffe are back on offense, while Tom Hajek, Peter Jacobs and Rob Van Beek are the top protected players on defense, with Matt Roik in goal.';
$capture = 5;
echo 'START: '.implode(' ', array_slice($words = explode(' ', $text), 0, $capture))."<br>n";
echo 'END: '.implode(' ', array_slice($words, $capture));
?>[/code]
Should it be necessary to maintain newlines and such in the new strings, then you might have to use something like my proposed solution.[/QUOTE]You could use a zero width positive lookaround to do that and then [I]implode()[/I] without any [I]glue[/I]! [B]
[CODE]/(?<=s(?=w))/[/CODE]
[/B][code=php]$first_five_words = implode(array_slice(preg_split('/(?<=s(?=w))/', $text), 0, 5));[/code]
[code=php]$text="The Philadelphia Wings finished the 2006 regular season with an 8-8 record, but missed the yada yada yada...";
preg_match("/(w*s){5}/",$text,$match);
echo"$match[0]";[/code]
[code=php]<?
$string = explode(" ", $string, 5);
$string = implode(' ', $string);
echo $string;
?>[/code]
0.1.9 — BETA 6.15