Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP + Regex to pull sections of a string after a specific pattern
    text
    copied!<p>Trying to figure this out and if there is another method aside from regex, I am open to it. </p> <p>Need to take a pattern similar to the following: </p> <ul> <li>One has spaces between the dash and the other does not. </li> <li>Sometimes there may be 3 periods and sometimes 4. </li> <li><p>Between the periods will always be numbers which may vary such as 1.111.1</p> <ol> <li>1.1.1-50</li> <li>1.1.1 - 50</li> <li>1.1.1- 50</li> <li>1.1.1 -50</li> </ol></li> </ul> <p>The above should output to:</p> <ul> <li>string1: 1.1.</li> <li>string2: 1</li> <li>string3: 50</li> </ul> <p>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. </p> <p><strong>Update:</strong> Complete and Working Code</p> <p>Utilized the information provided by hakre and Niels and created the following code:</p> <p>Not sure if my code is optimized but this is basically what I need to accomplished. </p> <pre><code>&lt;form action="" method="post"&gt; &lt;p&gt; &lt;strong&gt;Records Range:&lt;/strong&gt; &lt;input type="text" name="records_range" size="30" maxlength="22" /&gt; &lt;br /&gt; &lt;strong&gt;Internal ID:&lt;/strong&gt; &lt;input type="text" name="internal_id" size="40" /&gt; &lt;select name="id_options"&gt; &lt;option value="default_internal_id"&gt;Default Internal ID&lt;/option&gt; &lt;option value="new_internal_id"&gt;New Internal ID&lt;/option&gt; &lt;/select&gt; &lt;br /&gt; &lt;input type="submit" value="Generate" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;?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 "&lt;p&gt;&lt;strong&gt;Record Data Generated For:&lt;/strong&gt; ".$range_prefix.$range_start." - ".$range_end."&lt;/p&gt;"; } switch ($id_options){ case 'default_internal_id': echo "&lt;textarea cols=\"65\" rows=\"10\"&gt;"; // start output while($range_start &lt;= $range_end){ if($range_start &lt; $range_end){ echo "EUI-ZQ50-N-".$range_prefix.$range_start."\n"; } else{ echo "EUI-ZQ50-N-".$range_prefix.$range_start; } $range_start++; } echo "&lt;/textarea&gt;"; break; case 'new_internal_id': echo "&lt;textarea cols=\"65\" rows=\"10\"&gt;"; // start output while($range_start &lt;= $range_end){ if($range_start &lt; $range_end){ echo $internal_id." ".$records_prefix.$range_start"\n"; } else{ echo $internal_id." ".$records_prefix.$range_start; } $range_start++; } echo "&lt;/textarea&gt;"; break; default: echo "&lt;h4&gt;Example:&lt;/h4&gt;"; echo "&lt;p&gt;&lt;strong&gt;Records Range&lt;/strong&gt;: 1.22.333.444-500 = 1.22.333.444 &lt;strong&gt;THROUGH&lt;/strong&gt; 500&lt;/p&gt;"; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload