BASIC PARSING OF VALUES WITHIN STRINGS

Sometimes we need to parse and extract individual values from within a string.
This can be done using the PHP list() and preg_split() operations.

******************************************************************************
EXAMPLE 1:

Calling the PHP function to return the date elements corresponding to a given
Gregorian calendar JD (Julian Day) number returns the date elements separated
by slash (/) characters, such as 2/24/1925 for Month/Day/Year.

$mdy = JDtoGregorian(2433070);

would return the ($mdy) string holding the values:
6/2/1949

Notice that there are three numerical values returned, separated by slash (/)
characters.

These values represent the month, day and year values respectively derived
from the given JD number.

To parse the individual returned values and put them into their respective
variables, ($m,$d,$y) we can use the list() and preg_split() operations
as demonstrated in the following PHP code block.

______________________________________________________________________________

<?php

$JDNum  = 2433070;
$mdyStr = JDtoGregorian($JDNum);

list($m, $d, $y) = preg_split("[\/]", $mdyStr);

print
"<pre>
JD = $JDNum
m  = $m
d  = $d
y  = $y
</pre>";

?>
______________________________________________________________________________


Example 1 will define and print the following variables parsed from the string
returned by the JDtoGregorian() function call.

JD = 2433070
m  = 6
d  = 2
y  = 1949




******************************************************************************
******************************************************************************
EXAMPLE 2:

Suppose we have a function that returns the spatial XYZ-coordinates of a point
and we need to extract those coordinates into their respective variables.
The following code block shows how to do this. The same technique can be used
for any number of variables.

______________________________________________________________________________

<?php

$XYZStr = '1.12345 -2.34567 3.45678';

list ($x, $y, $z) = preg_split("[ ]", $XYZStr);

print
"<pre>
x = $x
y = $y
z = $z
</pre>";

?>
______________________________________________________________________________

Example 2 will will define and print the following variables parsed from the
string containing the space delimited XYZ-coordinates.

x = 1.12345
y = -2.34567
z = 3.45678