Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Text & Number in a string (then split it).

I'm sure this has been asked before but after some googling I decided I'd ask for help since I'm a novice to php.

Currently I am wanting to find a username and the date in a text file.

Example of text file:
Code
User1 - 2014-07-21
User2 - 2014-07-25

I thought if I used "preg_match_all()" to find the line I would be able to then split the "User" from the "Date" by using another "preg_match_all()" but it seems that the function can't find the text for the user and date.

I thought it would be as easy as this:

Code
$user = "Deko"; //$_POST['u'];
$input = "Deko - 2014-07-21"; //Using a string for testing

preg_match_all('/'.$user.' - !\d+-\d+-\d+!/', $input, $output);
$output = implode(' ', $output[0]);

preg_match_all('/'.$user.'/', $output, $cUser);
$cUser = implode(' ', $cUser[0]);

preg_match_all('!\d+-\d+-\d+!', $output, $cDate);
$cUser = implode(' ', $cDate[0]);

but apparently it isn't. I guess any help is appreciated since I have little to no idea of what I'm doing. Feel free to criticize (no assholes though) and tell me how I can get better.
Is this close to what you want?
PHP Code

<?PHP
$a
=file_get_contents('./users.txt');
$b=explode('\n', $a);
for(
$i=0;isset($b[$i]);$i++)
{
    
$c=explode(" - ", $b[$i]);
    
$users[]=trim($c[0]);
    
$dates[]=trim($c[1]);
}
?>
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
(Jul 21, 2014 04:00 AM)Squito Wrote:  Is this close to what you want?
PHP Code

<?PHP
$a
=file_get_contents('./users.txt');
$b=explode('\n', $a);
for(
$i=0;isset($b[$i]);$i++)
{
    
$c=explode("-", $b[$i]);
    
$users[]=trim($c[0]);
    
$dates[]=trim($c[1]);
}
?>

That might just be it. I'll have to seperate the user and date with '|' as using '-' in the explode would cause the code to skip the month/day of the date.

I'll test it and get back to you. Thank you.
Updated the code, it will fix that problem.
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
That works too :)

After testing out the code, I can confirm it doesn't work for exactly what I need.

I added:

Code
$v_users = implode(' ',$users);
$v_dates = implode(' ',$dates);

to the code you posted and printed the $v_users and $v_dates, turns out the $users only reads up until the first " - " and the $dates only reads from the " - " to the following " - " (including the next user).
Could you post or PM me the text file you are working with?
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
(Jul 21, 2014 04:51 AM)Squito Wrote:  Could you post or PM me the text file you are working with?

I've PM'd you. Hopefully this can be solved soon.
PHP Code

<?php 
$users 
= $dates = array();
foreach(
file('file.txt') as $line){
    
$exp = explode(' - ',$line);
    
$users[] = $exp[0];
    
$dates[] = $exp[1];
} 
(Jul 21, 2014 07:09 AM)RedWaters Wrote:  
PHP Code

<?php 
$users 
= $dates = array();
foreach(
file('file.txt') as $line){
    
$exp = explode(' - ',$line);
    
$users[] = $exp[0];
    
$dates[] = $exp[1];
} 

Hmm, I have just realized that this isn't what I want because I'm wanting to search for the 'user' and then the date that's on the same line as the 'user'

Example:

My user is: Deko

Text File:
Code
Subject75 - 2014-07-21
Subject75 - 2014-07-21
Subject23 - 2014-07-22
Subject91 - 2014-07-22
Deko - 2014-07-25
Subject85 - 2014-07-28
Subject34 - 2014-07-29

I want to find 'Deko', once found, grab that line and use that one date as a variable.

PHP Code

<?php 
$user 
= $_POST['u']
$file = "users.txt" 
(Jul 21, 2014 07:28 AM)Deko Wrote:  Hmm, I have just realized that this isn't what I want because I'm wanting to search for the 'user' and then the date that's on the same line as the 'user'

Example:

My user is: Deko

Text File:
Code
Subject75 - 2014-07-21
Subject75 - 2014-07-21
Subject23 - 2014-07-22
Subject91 - 2014-07-22
Deko - 2014-07-25
Subject85 - 2014-07-28
Subject34 - 2014-07-29

I want to find 'Deko', once found, grab that line and use that one date as a variable.

PHP Code

<?php 
$user 
= $_POST['u']
$file = "users.txt" 

PHP Code

<?php 
$users 
= array();
foreach(
file('file.txt') as $line){
    
$exp = explode(' - ',$line);
    
$users[$exp[0]] = $exp[1];
}

if(isset(
$users['Deko'])) //to use like $users[$_POST['u']]
    
echo $users['Deko']; //date
else
    echo 
'No deko :((('; 

This is an associative example, the username will be the key and the date the value
(Jul 21, 2014 07:38 AM)RedWaters Wrote:  
PHP Code

<?php 
$users 
= array();
foreach(
file('file.txt') as $line){
    
$exp = explode(' - ',$line);
    
$users[$exp[0]] = $exp[1];
}

if(isset(
$users['Deko'])) //to use like $users[$_POST['u']]
    
echo $users['Deko']; //date
else
    echo 
'No deko :((('; 

This is an associative example, the username will be the key and the date the value

I think is exactly what I wanted. Give me 5 minutes to test and I'll report back :)

EDIT: After testing and changing a few lines to fit my system, I can say that your method works perfectly. Thank you very much!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by TetraHCSep 4, 2019 03:12 AM 9 221 Sep 4, 2019 03:12 AM
Last Post: newQuery
[Javascript]  by v1zionApr 27, 2018 01:10 AM 3 148 Apr 27, 2018 01:10 AM
Last Post: V3t3r4n
[PHP] by deadsh0tJan 31, 2018 06:45 AM 3 149 Jan 31, 2018 06:45 AM
Last Post: deadsh0t