-1

In a txt file, there's the following:

Field1:Field2:Field3:Field4
Data1:Data2:Data3:Data4

How to get the text which is just after "Field1" which is "Field2" and without getting the ":" which is before "Field3" using PHP ?

NOTICE: "Field" won't be a constant string.

EDIT: I want to do something like searching for "Field2" and doing that on it's line only. So I don't want to do that for "Data" too ! Thanks.

Jacksweet
  • 17
  • 6
  • 1
    You could checkout [`explode()`](http://php.net/manual/en/function.explode.php) and give it a shot... Btw, what do you mean with your notice? – M. Eriksson Feb 07 '17 at 16:35
  • @MagnusEriksson - Any examples? – Jacksweet Feb 07 '17 at 16:35
  • Thee are a few examples on that link (click on the explode()-word). Just read them, give it a shot, and if you get stuck, come back and show us what you tried. This isn't a free coding service. – M. Eriksson Feb 07 '17 at 16:36
  • @MagnusEriksson - Would it be like http://pastebin.com/edT7j1T9 ? ... Can I do that for "Field" only and not for "Data" ? – Jacksweet Feb 07 '17 at 16:40
  • First explode on "\n" to get an array element for each line, then explode each line you wish with ":" separator... ? – Random Feb 07 '17 at 16:42
  • Are you asking me if your code works? Just try it. If it does what you want, then it works. If it doesn't, try something else. – M. Eriksson Feb 07 '17 at 16:42
  • **EDIT:** I want to do something like searching for "Field2" and doing that on it's line only. So I don't want to do that for "Data" too ! Thanks. – Jacksweet Feb 07 '17 at 16:43
  • @Random - Any examples? I'm really beginner on PHP :( – Jacksweet Feb 07 '17 at 16:45
  • 1
    http://stackoverflow.com/help/how-to-ask – The Bearded Llama Feb 07 '17 at 16:50
  • 1
    We can't code for you, you should take some lessons if you are not able to test such a simple function. The link @MagnusEriksson gave is all you need to code, with many examples and explanations of how it works... – Random Feb 07 '17 at 16:53
  • Not sure this is even a good specification – RiggsFolly Feb 07 '17 at 16:53

1 Answers1

0

EDIT: The main goal I wanted to ask this question for is:


IGNORE IF YOU WANT THE ANSWER, THE ANSWER IS BELOW:


I'm developing an AdminCP for my project, and if the administrator typed "User1" in a HTML form in the AdminCP it must return "This user is suspended!" if the line in the txt file was "User1:suspended:24/01/2012" .

While the format of users in the txt file is:

USERNAME:STATUS:REGISTRATION DATE
User1:suspended:24/01/2012

(All the texts and strings are just examples).

So in the below answer ... $searchfor = 'Field2'; must be $searchfor = $_POST['username']; for example.

And I wanted to use:

if( $data[1]=="suspended" ) {
    echo "The user which owns ".$data[0]." is suspended!";
}

While $data[0] is the username (User1), and $data[1] is the status (suspended).


Thanks to @MagnusEriksson and PHP to search within txt file and echo the whole line . I just discovered the solution by myself:

<?php
$file = 'test.txt';
$searchfor = 'Field2';

// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
preg_match_all($pattern, $contents, $matches);

$wholeLine = implode("\n", $matches[0]);
$data = explode(":", $wholeLine);
echo $data[0]; // Field1
echo $data[1]; // Field2
?>
Community
  • 1
  • 1
Jacksweet
  • 17
  • 6