homepage Welcome to WebmasterWorld Guest from 54.147.248.118
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Regular Expression.can it be done?
alphacooler

5+ Year Member



 
Msg#: 3383164 posted 11:13 pm on Jun 30, 2007 (gmt 0)

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?

 

darrenG

5+ Year Member



 
Msg#: 3383164 posted 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]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved