Re: Strip alternating spaces in first word of paragraph
| From: | Burhan Khalid | Date: | Wed, 23 Feb 2005 15:11:03 +0000 |
| Subject: | Re: Strip alternating spaces in first word of paragraph | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-209239@lists.php.net to get a copy of this message | ||
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?This is not a solution, but something you can start off with. I haven't error checked it, and its probably not the most elegant way of doing it, but oh well : <?php $string = 'E n d l e s s insanity here!'; /* This gives us an array where the index is
the position at which the word was detected.
My idea is that if the spaces are a single
digit apart, then we track them; assuming
that the first "short" word is a two letter
word, such as "An".
However, this won't work for paragraphs
that begin with An, A, etc.
*/
$letters = explode(" ",$string);
foreach ($letters as $key => $letter)
{
if (strlen($letter) == 1)
{
//Possible letter of our first word
$word .= $letters[$key];
} else {
$split_point = $key;
}
}
echo "Original : ".$string."\n";
echo "Word : ".$word."\n";echo "Modified : ".$word." "; echo implode(" ",array_slice($letters,$split_point-1))."\n"; ?> Gives me : $ php -q string.php Original : E n d l e s s insanity here!
Word : EndlessModified : Endless insanity here!