Re: Strip alternating spaces in first word of paragraph

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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     : Endless
Modified : Endless insanity here!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux