Jump to content

preg_match_all from string to string? fopen


Paulqvz

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";
}

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 2 weeks later...
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.