fastsol Posted February 6, 2015 Share Posted February 6, 2015 I have a database with vehicle info that I want to split some of the text off the end of the vehicle name. Example: Subaru WRX 2010 Manual Trans I want to split it into 2 parts like this array(0 => Subaru WRX 2010, 1 => Manual Trans) So basically I want to split it right after the year in the string. Now the other problem is that the year is not always the 3rd thing in the name. Another example would be Chevy SIlverado (standard key) 2011 Automatic. I tried preg_split but I loose the year in the returned array cause I was trying something like this $parts = preg_split('/[0-9]{4}/', $row['vehicle']); Like many people I am terrible at regex. I have watched some videos and messed around for a while but can't figure out how to split it AFTER the 4 digit year only. Any help is appreciated! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 6, 2015 Share Posted February 6, 2015 the split pattern is not included, unless you use the PREG_SPLIT_DELIM_CAPTURE flag, but that will give you an array with three entries, that you must combine the first two back together. you should actually use preg_match_all() with a pattern that gets everything up to and including the 4 digit number and everything after the 4 digit number. based on my limited regex knowledge, this does what you are asking - preg_match_all('/(.*[0-9]{4})\s+(.*)/', $row['vehicle'],$parts); echo 'up to and including the 4 digit number - ' . $parts[1][0] . '<br>'; echo 'after the 4 digit number - ' . $parts[2][0]; Quote Link to comment Share on other sites More sharing options...
fastsol Posted February 6, 2015 Author Share Posted February 6, 2015 (edited) Thank you. I think I may have left out a critical part to the examples, seeing how things are returned in your code. The thing is that not every row will have words after the 4 digit year. So testing with your code like this foreach($rows as $row) { $parts = preg_split('/[0-9]{4}/', $row['vehicle']); //print_r($parts); preg_match_all('/(.*[0-9]{4})\s+(.*)/', $row['vehicle'],$parts); print_r($parts); //echo 'up to and including the 4 digit number - ' . $parts[1][0] . '<br>'; //echo 'after the 4 digit number - ' . $parts[2][0]; echo '<br><br>'; } I end up with an array like this, missing all info for cars that don't have words after the year. Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( [0] => Nissan Quest 2014 PTS ) [1] => Array ( [0] => Nissan Quest 2014 ) [2] => Array ( [0] => PTS ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( [0] => Subaru Legacy 2014 Manual Trans ) [1] => Array ( [0] => Subaru Legacy 2014 ) [2] => Array ( [0] => Manual Trans ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( [0] => Infiniti M35 / M45 2007 PTS ) [1] => Array ( [0] => Infiniti M35 / M45 2007 ) [2] => Array ( [0] => PTS ) ) Array ( [0] => Array ( [0] => Toyota Highlander 2015 PTS ) [1] => Array ( [0] => Toyota Highlander 2015 ) [2] => Array ( [0] => PTS ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( [0] => Ford Mustang 2013 Manual Trans ) [1] => Array ( [0] => Ford Mustang 2013 ) [2] => Array ( [0] => Manual Trans ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( [0] => Toyota Prius 2005 PTS ) [1] => Array ( [0] => Toyota Prius 2005 ) [2] => Array ( [0] => PTS ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) Array ( [0] => Array ( [0] => Honda Accord (standard key) 2014 Manual Trans ) [1] => Array ( [0] => Honda Accord (standard key) 2014 ) [2] => Array ( [0] => Manual Trans ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) ) I tried the PREG_SPLIT_DELIM_CAPTURE on the preg_split but that too for whatever reason did not return the year as a array element like it says it would. That resulted in this array (commenting out the preg_match_all() too) Array ( [0] => Chrysler 300 (standard key) [1] => ) Array ( [0] => Ford Fusion [1] => ) Array ( [0] => Nissan Quest [1] => PTS ) Array ( [0] => Jeep Liberty [1] => ) Array ( [0] => Pontiac Sunfire [1] => ) Array ( [0] => Subaru Legacy [1] => ) Array ( [0] => Buick Lucerne [1] => ) Array ( [0] => Saturn Ion [1] => ) Array ( [0] => Ford Escape (standard key) [1] => ) Array ( [0] => Chevrolet Cruze (flip key) [1] => ) Array ( [0] => Acura MDX [1] => ) Array ( [0] => Subaru Impreza [1] => ) Array ( [0] => Ford Focus (standard key) [1] => ) Array ( [0] => Toyota Corolla [1] => ) Array ( [0] => Nissan Murano [1] => ) Array ( [0] => Honda CR-V [1] => ) Array ( [0] => Toyota Yaris [1] => ) Array ( [0] => Chevrolet Silverado (standard key) [1] => ) Array ( [0] => Subaru Legacy [1] => Manual Trans ) Array ( [0] => Volkswagen Routan [1] => ) Array ( [0] => Ford Crown Vic [1] => ) Array ( [0] => Infiniti M35 / M45 [1] => PTS ) Array ( [0] => Toyota Highlander [1] => PTS ) Array ( [0] => Ford Edge (standard key) [1] => ) Array ( [0] => Nissan Titan [1] => ) Array ( [0] => Toyota RAV4 [1] => ) Array ( [0] => Chevrolet Avalanche [1] => ) Array ( [0] => Chevrolet Silverado (standard key) [1] => ) Array ( [0] => Toyota RAV4 [1] => ) Array ( [0] => Honda CR-V [1] => ) Array ( [0] => Ford Fusion [1] => ) Array ( [0] => Ford Mustang [1] => Manual Trans ) Array ( [0] => Dodge Ram Pickup [1] => ) Array ( [0] => Subaru Legacy [1] => ) Array ( [0] => Mercury Mountaineer [1] => ) Array ( [0] => Toyota Prius [1] => PTS ) Array ( [0] => Honda Pilot [1] => ) Array ( [0] => Pontiac G6 [1] => ) Array ( [0] => Jeep Wrangler [1] => ) Array ( [0] => Honda Accord (standard key) [1] => ) Array ( [0] => Kia Sorento [1] => ) Array ( [0] => Buick Rendezvous [1] => ) Array ( [0] => Mercury Marauder [1] => ) Array ( [0] => Honda CR-V [1] => ) Array ( [0] => Toyota Camry [1] => ) Array ( [0] => Mercury Sable [1] => ) Array ( [0] => Toyota FJ Cruiser [1] => ) Array ( [0] => Chrysler Concorde [1] => ) Array ( [0] => Honda Accord (standard key) [1] => Manual Trans ) Array ( [0] => Chevrolet HHR [1] => ) More help please! Edited February 6, 2015 by fastsol Quote Link to comment Share on other sites More sharing options...
fastsol Posted February 6, 2015 Author Share Posted February 6, 2015 (edited) I think I'm getting closer. Using this code foreach($rows as $row) { $parts = preg_split('/[0-9]{4}\s/', $row['vehicle']); //print_r($parts); //preg_match_all('/(.*[0-9]{4})\s+(.*)/', $row['vehicle'],$parts); print_r($parts); /*echo 'up to and including the 4 digit number - ' . $parts[1][0] . '<br>'; echo 'after the 4 digit number - ' . $parts[2][0]; */ echo '<br><br>'; } I now get the years on the rows that don't have any words after the year in the string, but still no years in the ones with words after the year. Array ( [0] => Chrysler 300 (standard key) 2005 ) Array ( [0] => Ford Fusion 2012 ) Array ( [0] => Nissan Quest [1] => PTS ) // No year here Array ( [0] => Jeep Liberty 2005 ) Array ( [0] => Pontiac Sunfire 2005 ) Array ( [0] => Subaru Legacy 2014 ) Array ( [0] => Buick Lucerne 2006 ) Array ( [0] => Saturn Ion 2004 ) Array ( [0] => Ford Escape (standard key) 2010 ) Array ( [0] => Chevrolet Cruze (flip key) 2014 ) Array ( [0] => Acura MDX 2013 ) Array ( [0] => Subaru Impreza 2015 ) Array ( [0] => Ford Focus (standard key) 2007 ) Array ( [0] => Toyota Corolla 2009 ) Array ( [0] => Nissan Murano 2004 ) Array ( [0] => Honda CR-V 2003 ) Array ( [0] => Toyota Yaris 2007 ) Array ( [0] => Chevrolet Silverado (standard key) 2004 ) Array ( [0] => Subaru Legacy [1] => Manual Trans ) // No year here Array ( [0] => Volkswagen Routan 2010 ) Array ( [0] => Ford Crown Vic 2008 ) Array ( [0] => Infiniti M35 / M45 [1] => PTS ) // No year here Array ( [0] => Toyota Highlander [1] => PTS ) // No year here Array ( [0] => Ford Edge (standard key) 2012 ) Array ( [0] => Nissan Titan 2007 ) Array ( [0] => Toyota RAV4 2014 ) Array ( [0] => Chevrolet Avalanche 2006 ) Array ( [0] => Chevrolet Silverado (standard key) 2008 ) Array ( [0] => Toyota RAV4 2013 ) Array ( [0] => Honda CR-V 2013 ) Array ( [0] => Ford Fusion 2006 ) Array ( [0] => Ford Mustang [1] => Manual Trans ) // No year here Array ( [0] => Dodge Ram Pickup 2014 ) Array ( [0] => Subaru Legacy 2013 ) Array ( [0] => Mercury Mountaineer 2005 ) Array ( [0] => Toyota Prius [1] => PTS ) // No year here Array ( [0] => Honda Pilot 2010 ) Array ( [0] => Pontiac G6 2007 ) Array ( [0] => Jeep Wrangler 2011 ) Array ( [0] => Honda Accord (standard key) 2011 ) Array ( [0] => Kia Sorento 2014 ) Array ( [0] => Buick Rendezvous 2005 ) Array ( [0] => Mercury Marauder 2004 ) Array ( [0] => Honda CR-V 2015 ) Array ( [0] => Toyota Camry 2008 ) Array ( [0] => Mercury Sable 2002 ) Array ( [0] => Toyota FJ Cruiser 2011 ) Array ( [0] => Chrysler Concorde 2004 ) Array ( [0] => Honda Accord (standard key) [1] => Manual Trans ) // No year here Array ( [0] => Chevrolet HHR 2011 ) Edited February 6, 2015 by fastsol Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted February 6, 2015 Solution Share Posted February 6, 2015 (edited) You can and do want to use preg_split() to split on the space. But, you need to use a lookbehind in the RegEx. That will find a space that is preceded by the four digit year without the year being consumed as part of the match. $cars = array( 'Subaru WRX 2010 Manual Trans', 'Ford Fusion 2012', 'Chevy Silverado (standard key) 2011 Automatic' ); foreach($cars as $car) { $parts = preg_split('/(?<=[0-9]{4}) /', $car); print_r($parts); echo "<br>\n"; } Output Array ( [0] => Subaru WRX 2010 [1] => Manual Trans ) Array ( [0] => Ford Fusion 2012 ) Array ( [0] => Chevy Silverado (standard key) 2011 [1] => Automatic ) Edited February 6, 2015 by Psycho 1 Quote Link to comment Share on other sites More sharing options...
fastsol Posted February 6, 2015 Author Share Posted February 6, 2015 Thank you so very much. I was trying to mess with the lookbehind last night but couldn't get the right thing to happen. You're awesome! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.