Forum Moderators: coopster

Message Too Old, No Replies

Changing order of words in a string based on regex

         

Joe Belmaati

10:27 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



I didn't really know where to start searching for this, so if already covered, then many apologies.

I am dealing with some large text files that contain lots of wine tasting notes. I am trying to come up with a clever construct that will enable me to shift a word in a line to the beginning of the line based on a regex that will identify a year. This is what I've come up with and it seems to work, however, I'm sure there's a better way. Any ideas..?


$string = 'Latour Pauillac 1995 Bordeaux';
$wine = explode(' ', $string);

for ($i = 0; $i < count($wine); $i++)
{
if (preg_match('#\d{4}#', $wine[$i]))
{
echo $wine[$i] . ' ';
}
}

for ($i = 0; $i < count($wine); $i++)
{
echo preg_replace('#\d{4}#', '', $wine[$i]) . ' ';
}

coopster

11:33 pm on Sep 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not sure what you are going for ... but if it is working, go with it ;)

$string = 'Latour Pauillac 1995 Bordeaux'; 
print preg_replace("/(.*) (\d{4}) (.*)/", "$2 $1 $3", $string);

Joe Belmaati

11:27 pm on Sep 16, 2005 (gmt 0)

10+ Year Member



Hi,
thank you very much for the code. It works as long as the year is the third element in the array in which case I guess

echo $wine[2] . ' ' . $wine[0] . ' ' . $wine[1] . ' ' . $wine[3];

would also work. The problem is that the text strings are formatted differently. In some strings the year will be the second element, in other it'll be the third, and so on...

coopster

5:17 pm on Sep 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The regex offered here will find it no matter where it is and move it to the front of the string. I'm not certain if that was what you were looking for, but if not you could modify the regular expression accordingly.

Joe Belmaati

9:45 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



That does not seem to be the case. If I change the string to

$string = 'Latour Pauillac Bordeaux 1995';

The output is:

Latour Pauillac Bordeaux 1995

jd01

3:06 am on Sep 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you may need to make the spaces optional, or remove them because if the number is at the beginning or the end of the string, the pattern will not match spaceNUMNUMNUMNUMspace.

$string = 'Latour Pauillac 1995 Bordeaux';
print preg_replace("/(.*)\s?(\d{4})\s?(.*)/", "$2 $1 $3", $string);

If you wanted to be sure you only matched four numbers by themselves, you might use the boundary flag:

$string = 'Latour Pauillac 1995 Bordeaux';
print preg_replace("/(.*)\s?(\b\d{4}\b)\s?(.*)/", "$2 $1 $3", $string);

Hope this helps.

Justin

Joe Belmaati

10:16 am on Sep 18, 2005 (gmt 0)

10+ Year Member



Yes,
that works flawlessly :D Thank you very much!
Joe