Jump to content

Recommended Posts

Hi all. 

 

I have a php file that i need to read contents from. I can search for a string but i am stumped on how to start from a point and to end at a point

Below is the payout of the file. I just want to return 

Paul1
    plist1: plist1
    plist2: plist2
    plist3: plist3
    plist4: plist4

and not the rest.

 

quote_lost_reason
	"": ""
	Price: Price
	Product: Product
	Other: Other
Paul1
	plist1: plist1
	plist2: plist2
	plist3: plist3
	plist4: plist4
Site Lighting
	Interior: Interior
	Exterior: Exterior
credit
	credit_note: Credit Note

 

here is my code so far

 

$file = '/var/www/html/***/custom/include/language/lang.en_us.lists.php';
$searchfor = "Paul1";
$array = file($file);
//print_r($array);

header('Content-Type: text/plain');
$contents = file_get_contents($file);

$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches:\n";
   echo implode("\n", $matches[0]);
}
else{
   echo "No matches found";
}

 

Forget regular expressions.

As a human being, how do you look at the contents of that file and decide what to take away from it? Describe the process in detail, step by step.

Once you've done that, try expressing the exact same thing in code.

hi there

the first thing i would do is search for the string. once i have found it i will count the amount of lines til the next entry without a tab spacing - knowing that would be my next header. then i would show these entries .

 

Was that good?

43 minutes ago, Paulqvz said:

once i have found it i will count the amount of lines til the next entry without a tab spacing

The problem with that approach is that once you've counted them, you then have to go back to store the N items. I would us this logic...

image.png.4a103948825d802f135ad93639793550.png

If you have any control over the format of your text file, you might want to consider something more convenient, such as an ini file format...

[quote_lost_reason]
''=
Price="Price"
Product="Product"
Other="Other"

[Paul1]
plist1="plist1"
plist2="plist2"
plist3="plist3"
plist4="plist4"

[Site Lighting]
Interior="Interior"
Exterior="Exterior"

[credit]
credit_note="Credit Note"

then your processing would reduce to

$data = parse_ini_file('myfile.ini', true);
$wanted = $data['Paul1'];

//wiew results
echo '<pre>' . print_r($wanted, 1) . '</pre>';

Array
(
    [plist1] => plist1
    [plist2] => plist2
    [plist3] => plist3
    [plist4] => plist4
)

 

Edited by Barand
  • 2 weeks later...

OK. Back to your original file format. I showed you the logic to use in your code. Have you even attempted that method yet? (it's only about six or seven lines of code)

If you at least try, you get more help.

Or are you just upset because no one wrote the actual code for you

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.