|
|
 |
RE: FN-FORUM: help with regular expressions please!
date posted 5th January 2007 17:47
Duncan Glendinning wrote:
> Right, I have a field in my database, which contains
> something of the form:
>
> Species:Cinnamonum zeylanicum
> Origin: Sri Lanka
>
> Now what I need to do is use some reg expressions to split
> the field's value into two, and save each to a different
> field, so I have:
>
> Species:Cinnamonum zeylanicum in one, and
> Origin: Sri Lanka in the other.
You could loop through the entire recordset in PHP and do something like the
following:
$str = "Species:Cinnamonum zeylanicum Origin: Sri Lanka" ;
$pattern = "/(Species|Origin):/" ;
$elements = preg_split( $pattern, $str ) ;
This will give you an array of the elements which you can then process
further as needed. The array would be:
Array
(
[0] =>
[1] => Cinnamonum zeylanicum
[2] => Sri Lanka
)
This basic pattern doesn't clean up the string and remove things like line
breaks (if there are any), or take into account whether there are case
differences in "Species:" or "species:". You'd need to extend the pattern to
do this or post-process the strings.
There may be quicker or alternative ways, but this will work via the regular
expression route.
HTH
Edward
|
 |
|