Jump to content

How can I select sections of a line to be saved as variables in a graphical way on a website?


Go to solution Solved by mac_gyver,

Recommended Posts

Ok, I guess the title will be confusing so let me elaborate. I have lines of data which will be in the same format each line and I need to identify each section of the line with a variable. For example, lets assume the below line is what I need to parse and store individual sections as variables:


01/12 12:00:12 1000 T301 01:89 A1234524


I understand how to do this using things like substr etc but what I am trying to achieve here is to find a way to present this line on a webpage and then have the user put markers of some sort at the beginning and end of each section in the line and save it as a variable name such as date, time etc.


The reason I want to do this is because the formatting of the line can change from one device to another depending on how it is configured, so I need a way for the users to easily provide the location of each portion of the data and I haven't been able to wrap my head around a way to do this as yet.


All suggestions welcomed! :)


if you want/need to do this on a character basis, i.e. you might want to get just part of the text out of a field and use substr() to parse the data, provide a series of form inputs (perhaps dynamically added) for the parameters that substr(), needs, i.e. the starting position and the length. each set of form fields would also have a form input to give a name to that particular piece of data.

 

after you submit and store the definition for a particular format, you would just loop over the defining sets of values - start, length, and name, supplying the start and length values to the substr() statement, and use the name as an array index to save the result under.

Oh, I am using a perl script that monitors a specific ip:port and then saves the data to a text file as well as update a database. Each portion of the line is substr() to a variable and the database is updated with these variables. What I am trying to do here is to allow the users to specify the exactly location of the variable within the line as it can vary depending on the device. I can provide details of how to adjust the substr() settings for each variable but I would love to be able to provide a simple way of doing it in a graphical way.

if you want/need to do this on a character basis, i.e. you might want to get just part of the text out of a field and use substr() to parse the data, provide a series of form inputs (perhaps dynamically added) for the parameters that substr(), needs, i.e. the starting position and the length. each set of form fields would also have a form input to give a name to that particular piece of data.

 

after you submit and store the definition for a particular format, you would just loop over the defining sets of values - start, length, and name, supplying the start and length values to the substr() statement, and use the name as an array index to save the result under.

I understand how to use the substr() to do the above, I just wish to be able to allow users to assign specific portions of the line to a preset variable name via a webpage.

I understand how to use the substr() to do the above, I just wish to be able to allow users to assign specific portions of the line to a preset variable name via a webpage.

 

 

 

the first paragraph in my reply describes a way of doing this.

 

here is a rough example - 

<?php
$sample = '01/12 12:00:12 1000 T301 01:89 A1234524';


$i = 0;
$j = 0;
$ones = '';
$tens = '';
$len = strlen($sample);
for($x = 0; $x < $len; $x++)
{
    $class = ($i % 2) ? 'odd_span' : 'even_span';
    $ones .= "<span class='$class'>".$i++."</span>";
    $tens .= "<span class='$class'>".$j."</span>";
    if($i > 9)
    {
        $i = 0;
        $j++;
    }
}
?>
<style>
.even_span { 
    background-color: springgreen; 
}
.odd_span { 
    background-color: orangered; 
}
</style>
<?php
echo '<pre>';

print_r($_POST);

// 'store' the just submitted data as the 'rules' to use to parse the data
$rules = $_POST['data']; // this would be retrieved from wherever you have actually stored the submitted rules
$result = []; // parsed data
foreach($rules as $rule)
{
    $result[$rule['name']] = substr($sample,$rule['start'],(int)$rule['length']);
}

print_r($result);

echo "$sample<br>";
echo $tens;
echo '<br>';
echo $ones;
echo '</pre>';

$options = '';
foreach(range(0,$len-1) as $position)
{
    $options .= "<option value='$position'>$position</option>";
}
?>
<form method='post'>
Format name: <input type='text' name='format_name'><br>
<table>
<tr><th>Start</th><th>Length</th><th>Name</th></tr>
<tr><td><select name='data[0][start]'><?php echo $options; ?></select></td>
<td><input type='number' name='data[0][length]'></td>
<td><input type='text' name='data[0][name]'></td>
</tr>
<tr><td><select name='data[1][start]'><?php echo $options; ?></select></td>
<td><input type='number' name='data[1][length]'></td>
<td><input type='text' name='data[1][name]'></td>
</tr>
</table>
<input type='submit'>
</form>
Edited by mac_gyver

 

the first paragraph in my reply describes a way of doing this.

 

here is a rough example - 

<?php
$sample = '01/12 12:00:12 1000 T301 01:89 A1234524';


$i = 0;
$j = 0;
$ones = '';
$tens = '';
$len = strlen($sample);
for($x = 0; $x < $len; $x++)
{
    $class = ($i % 2) ? 'odd_span' : 'even_span';
    $ones .= "<span class='$class'>".$i++."</span>";
    $tens .= "<span class='$class'>".$j."</span>";
    if($i > 9)
    {
        $i = 0;
        $j++;
    }
}
?>
<style>
.even_span { 
    background-color: springgreen; 
}
.odd_span { 
    background-color: orangered; 
}
</style>
<?php
echo '<pre>';

print_r($_POST);

// 'store' the just submitted data as the 'rules' to use to parse the data
$rules = $_POST['data']; // this would be retrieved from wherever you have actually stored the submitted rules
$result = []; // parsed data
foreach($rules as $rule)
{
    $result[$rule['name']] = substr($sample,$rule['start'],(int)$rule['length']);
}

print_r($result);

echo "$sample<br>";
echo $tens;
echo '<br>';
echo $ones;
echo '</pre>';

$options = '';
foreach(range(0,$len-1) as $position)
{
    $options .= "<option value='$position'>$position</option>";
}
?>
<form method='post'>
Format name: <input type='text' name='format_name'><br>
<table>
<tr><th>Start</th><th>Length</th><th>Name</th></tr>
<tr><td><select name='data[0][start]'><?php echo $options; ?></select></td>
<td><input type='number' name='data[0][length]'></td>
<td><input type='text' name='data[0][name]'></td>
</tr>
<tr><td><select name='data[1][start]'><?php echo $options; ?></select></td>
<td><input type='number' name='data[1][length]'></td>
<td><input type='text' name='data[1][name]'></td>
</tr>
</table>
<input type='submit'>
</form>

 

Interesting! I will give this a try and see if I can make it work for me, thanks!

Ah I see what you did there, I did that as well (without the pretty colours) but unfortunately it proved difficult for some people reading the numbers correctly... So what I am trying to do would be to highlight a portion of the line and select a variable name from a list for that section.

  • Solution

if you want to do a 'click' interface, you would use javascript, but the data you need to produce and submit needs to be similar/same to what is given in the example above (you could have a start and end instead and calculate the length when the data is received by the php code.)

 

if you output each character of the sample string in its own span, with a common name + numerical id that indicates the position, when you click on a character, you can get numerical position from the id of the DOM element that was clicked. if there is no currently selected id, you would start a 'pick' sequence. the first pick would give the start value. a second pick would give the end value and you would calculate the length from the difference between the end and start values. you would populate the form fields with these values. you can either use a select/option menu to pick a name or click on list of displayed names to populate the name form field. once you have picked values for all three fields in a set, the logic would 'reset' and any picks would start the process for the next set of values. you can change the color of any picked character when it is clicked on and you can change all the characters between the start and end positions if you want.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.