Jump to content

Recommended Posts

So, I have a text file snippet that is entered into a textarea and upon submission, I'd like to search and define (if applicable) certain variables.  For example, if SDT is present, I'd like to assign the $sdt variable.  My problem is I don't know how to explode even if I'm supposed to explode it.  There are about 8 different variables I need to search for and I don't want to screw it up by exploding too many times or something.

Link to comment
https://forums.phpfreaks.com/topic/268526-trying-to-create-variables-from-a-_post/
Share on other sites

Regular expressions are typically slow compared to other options. String functions *could* be the best solution here.

 

@refiking

 

You need to provide an example of what the input looks like for us to even begin to provide a possible solution. Are there line breaks between each proposed variable? Can the values have spaces and/or line breaks? Too many possibilities to even begin to suggest something.

Honestly, I'm not sure exactly what approach I need to take.  I know for example there is the SDX id.  i don't know how to do a search for only that SDX.  Then, there's OPSYS ($ops).  It has OPY and then a space and then the operating system, but it also contains the build info that I wouldn't want, everything before it, though.  What would you recommend I use for pulling out the variables I need?  Remember, there might be things before and after it that I wouldn't want to be a part of the value.

@psycho...There are line breaks throughout the text, but not between the variable where the values are located.

I'm looking to extract the following:

SDX, telephone number, opsys, physical memory size, c drive size, and physical address

Timmy Cramer
SDX6052
TEL 5207426818
System Information

--------------------------------------------------------------------------------------------
OPSYS                       Windows 7 Home Premium x64 Edition 6.1 (build: 7601) Service Pack 1
BIOS Version             HPQOEM - 1
Last booted              9/15/2012 1:45 PM
Computer Name            OWNER-HP
Computer Description     owner-HP
Interactive User         owner-HP\owner
Default Browser          Internet Explorer
Default Browser Path     C:\Program Files (x86)\Internet Explorer\iexplore.exe
Default Browser Version  9.00.8112.16448
Device screen            1024 x 768 x 32


CPU Information

-----------------------------------------------------
CPU Name         AMD
CPU Clock        1497 MHz
CPU Description  Number 4 Level 18 Model 1 Stepping 0
CPU Usage        27%


Memory Information

-------------------------------
Physical Memory Size    3.47 GB
Physical Memory Used    1.75 GB
Physical Memory Used %  50%
Physical Memory Free    1.72 GB
Physical Memory Free %  49%
Commit Memory Size      6.95 GB
Commit Memory Used      2.37 GB
Commit Memory Used %    34%
Commit Memory Free      4.57 GB
Commit Memory Free %    65%


Disk Drives

Drive  Size       Free       % in use
-------------------------------------
C:\    441.51 GB  390.39 GB  12%
D:\    20.08 GB   2.17 GB    90%
E:\    3.95 GB    1.08 GB    73%
F:\                          0%
G:\                          0%
Q:\                          0%


Processes (Top 5 per CPU)

Name                 CPU%  Memory
------------------------------------
System Idle Process  73%
iexplore.exe         9%    337.43 MB
LMI_Rescue_srv.exe   3%    79.74 MB
iexplore.exe         3%    287.75 MB
System               2%


Events (Last 5 Errors)

Type  Generated          Source                        Event ID
---------------------------------------------------------------
      9/15/2012 6:48 PM  Microsoft-Windows-DNS-Client  1014
      9/15/2012 6:29 PM  Microsoft-Windows-DNS-Client  1014
      9/15/2012 5:57 PM  Microsoft-Windows-DNS-Client  1014
      9/15/2012 5:15 PM  VSS                           12348
      9/15/2012 2:58 PM  CVHSVC                        100


Scheduled Tasks (Last 5)

Name  Last run  Next run  Status
Connection-specific DNS Suffix: mifi.local
Description: Atheros AR9485 802.11b/g/n WiFi Adapter
Physical Address: ‎XX-XX-XX-XX-XX-XX
DHCP Enabled: Yes
IPv4 Address: 192.168.1.2
IPv4 Subnet Mask: 255.255.255.0
Lease Obtained: Saturday, September 15, 2012 5:15:20 PM
Lease Expires: Sunday, September 16, 2012 5:15:20 PM
IPv4 Default Gateway: 192.168.1.1
IPv4 DHCP Server: 192.168.1.1
IPv4 DNS Server: 192.168.1.1
IPv4 WINS Server: 
NetBIOS over Tcpip Enabled: Yes
IPv6 Address: xxxxxxxxxx:2095
Temporary IPv6 Address: xxxxxxxx:6086:9f44
Link-local IPv6 Address: xxxxxxx:2095%13
IPv6 Default Gateway: xxxxxxxxxxxxx:507%13
IPv6 DNS Servers: 2001: xxxxxxxxxxxxxxxx:a0:d:0:3

Now, was that so hard to provide the data example?

 

Here you go. The code at the bottom will extract the data you want into an array with named indexes. It is a fully functioning script. Go ahead and verify it does what you need,then incorporate the relevant parts into your existing script.

 

Here is the output array generated using the sample data you provided:

Array
(
    [sDX] => 6052
    [TEL] => 5207426818
    [OPSYS] => Windows 7 Home Premium x64 Edition 6.1 (build: 7601) Service Pack 1
    [Physical Memory Size] => 3.47 GB
    [C:\] => 441.51 GB
    [Physical Address] => : ‎XX-XX-XX-XX-XX-XX
)

 

 

<?php 

if(isset($_POST['data']))
{
    $params = array('SDX', 'TEL', 'OPSYS', 'Physical Memory Size', 'C:\\', 'Physical Address');
    $result = array(); //Ouput array

    $lines = explode("\n", $_POST['data']);
    foreach($lines as $line)
    {
        foreach($params as $param)
        {
            if(strpos($line, $param)===0)
            {
                //Get the value
                $value = trim(substr($line, strlen($param)));
                //Add special handling as needed for some values
                if($param=='C:\\')
                {
                    $value = substr($value, 0, strpos($value, 'GB')+2);
                }
                //Add value to result array
                $result[$param] = $value;
                //Remove found key
                unset($params[array_search($param, $params)]);
            }
        }
    }
}


?>
<html>
<head></head>
<body>

<pre><?php if(count($result)) { print_r($result); } ?></pre>


<form action="" method="post">
Paste Data:<br>
<textarea name="data" style="width:800px;height:400px"></textarea><br>
<button type="submit">Submit</button>
</form>

</body>
</html>

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.