|
Posted by Richard Lynch on 02/23/05 19:10
Aaron Gould wrote:
> I have a series of paragraphs in which the first word contains
> alternating spaces. How can I strip out the spaces of only this first
> word, while leaving the remainder of the paragraph untouched?
>
> Here's an example:
>
> T h i s is the first paragraph. Only the first word is wacky.
>
> A n o t h e r paragraph with a wacky first word! Strange stuff...
>
> E n d l e s s insanity here!
Untested code:
<?php
$text = ltrim($text); //Just in case a leading space gets in there...
//Convert Mac/PC/Unix newlines:
$text = str_replace("\r\n", "\n", $text);
$text = str_replace("\r", "\n", $text);
$paragraphs = explode("\n", $text);
$new_text = '';
while (list(, $paragraph) = each($paragraphs)){
for ($second_word_second_letter = 1; $second_word_second_letter <
count($paragraph) && $paragraph[$second_word_second_letter] != ' ';
$second_word_second_letter += 2){
//an empty body with no code on purpose. see above
}
$first_word_end = $second_word_second_letter - 2;
$first_word_end = max(0, $first_word_end);
$first_word = substr($text, 0, $first_word_end);
$rest = substr($text, $first_word_end + 1);
$first_word = str_replace(' ', '', $first_word);
$new_text .= trim("$first_word $rest") . "\n";
}
?>
I think I've avoided all the ways a blank paragraphs would get bogus
results, but...
--
Like Music?
http://l-i-e.com/artists.htm
Navigation:
[Reply to this message]
|