Trying to figure this out and if there is another method aside from regex, I am open to it.
Need to take a pattern similar to the following:
- One has spaces between the dash and the other does not.
- Sometimes there may be 3 periods and sometimes 4.
Between the periods will always be numbers which may vary such as 1.111.1
- 1.1.1-50
- 1.1.1 - 50
- 1.1.1- 50
- 1.1.1 -50
The above should output to:
- string1: 1.1.
- string2: 1
- string3: 50
I can't figure out how to just choose the number between the last period and the dash, choose the numbers after the dash, and also ignore any white spaces.
Update: Complete and Working Code
Utilized the information provided by hakre and Niels and created the following code:
Not sure if my code is optimized but this is basically what I need to accomplished.
<form action="" method="post">
<p>
<strong>Records Range:</strong> <input type="text" name="records_range" size="30" maxlength="22" />
<br />
<strong>Internal ID:</strong> <input type="text" name="internal_id" size="40" />
<select name="id_options">
<option value="default_internal_id">Default Internal ID</option>
<option value="new_internal_id">New Internal ID</option>
</select>
<br />
<input type="submit" value="Generate" />
</p>
</form>
<?php
$id_options = NULL;
if (isset($_POST['records_range'])) {
$id_options = $_POST['id_options'];
$internal_id = strip_tags(trim(($_POST['internal_id'])));
$records_range = strip_tags(trim($_POST['records_range']));
preg_match('~^((?:\d+\.){2,3})(\d+)\s?-\s?(\d+)$~', $records_range, $record_segements);
$range_prefix = $record_segements[1];
$range_start = $record_segements[2];
$range_end = $record_segements[3];
echo "<p><strong>Record Data Generated For:</strong> ".$range_prefix.$range_start." - ".$range_end."</p>";
}
switch ($id_options){
case 'default_internal_id':
echo "<textarea cols=\"65\" rows=\"10\">";
// start output
while($range_start <= $range_end){
if($range_start < $range_end){
echo "EUI-ZQ50-N-".$range_prefix.$range_start."\n";
}
else{
echo "EUI-ZQ50-N-".$range_prefix.$range_start;
}
$range_start++;
}
echo "</textarea>";
break;
case 'new_internal_id':
echo "<textarea cols=\"65\" rows=\"10\">";
// start output
while($range_start <= $range_end){
if($range_start < $range_end){
echo $internal_id." ".$records_prefix.$range_start"\n";
}
else{
echo $internal_id." ".$records_prefix.$range_start;
}
$range_start++;
}
echo "</textarea>";
break;
default:
echo "<h4>Example:</h4>";
echo "<p><strong>Records Range</strong>: 1.22.333.444-500 = 1.22.333.444 <strong>THROUGH</strong> 500</p>";
}
?>