Welcome to WebmasterWorld Guest from 54.161.110.186

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regular Expression.can it be done?

     
11:13 pm on Jun 30, 2007 (gmt 0)

10+ Year Member



I'm a n00b when it comes to regular expressions. I can do the basics, but need some help with this one.

Say I have a paragraph with the following sentence:

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

I want to be able to pull out all the comma separated words/phrases and then re-order them and put them back into the paragraph..

So I would end up with something like:

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

Any ideas?

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

5+ Year Member



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]

 

Featured Threads

Hot Threads This Week

Hot Threads This Month