Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Regular Expression.can it be done?


darrenG - 10:17 am on Jul 1, 2007 (gmt 0)


If you know that the string will always be in the format:

[opener][list of items].[result or outcome].

Then you could perform the following (not tested so might need tinkering):

$str = 'You need to replace the carburetor, transmission, spark plugs, both driver and passenger mirrors, and tailights. This should come out to around $800.;

//seperate sentences
$sentences = explode('.', $str);

//seperate items
$items = explode(',', $sentences[0]);

//You now have:
//items[0] = 'You need to replace the carburetor';
//items= ' transmission';
//...and so on

$len = count($items) - 1;
$items[0] = substr($items[0], 0, strpos('the') + 4);
$items[] = substr($items[0], strpos('the') + 3);
$items[] = substr($items[$len], strpos('and') + 3);
$string = $items[0];
$items[0] = '';
shuffle($items);

for($i = 1; $i <= $len; $i++) {
if($i == $len - 1) {
$string .= $items[$i];
}

elseif($i == $len) {
$string .= ' and'.$items[$i];
}
else
$string .= $items[$i].',';
}

$string = $string.'.'.$sentences[1];

[1][edited by: darrenG at 10:45 am (utc) on July 1, 2007]


Thread source:: http://www.webmasterworld.com/php/3383164.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com