Paulqvz Posted September 27, 2021 Share Posted September 27, 2021 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/ Share on other sites More sharing options...
requinix Posted September 27, 2021 Share Posted September 27, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590376 Share on other sites More sharing options...
Paulqvz Posted September 27, 2021 Author Share Posted September 27, 2021 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? Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590377 Share on other sites More sharing options...
Paulqvz Posted September 27, 2021 Author Share Posted September 27, 2021 how do i after i find the expression string stop after the last line with a tab? Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590378 Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 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... Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590379 Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 (edited) 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 September 27, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590387 Share on other sites More sharing options...
Paulqvz Posted October 7, 2021 Author Share Posted October 7, 2021 no i do not have controll over the format - i must say i have posted here 3 times and not once was it helpfull. why is this forum still active as no one can help with a smal problem like this? Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590750 Share on other sites More sharing options...
Barand Posted October 7, 2021 Share Posted October 7, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590753 Share on other sites More sharing options...
Paulqvz Posted October 7, 2021 Author Share Posted October 7, 2021 wel i tried lots of different code - see my examples on top. Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590760 Share on other sites More sharing options...
Barand Posted October 7, 2021 Share Posted October 7, 2021 But have you tried the method I flowcharted for you? Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590762 Share on other sites More sharing options...
mac_gyver Posted October 7, 2021 Share Posted October 7, 2021 or you can just use a database and write a simple sql query to find and retrieve the data that you want. Quote Link to comment https://forums.phpfreaks.com/topic/313818-preg_match_all-from-string-to-string-fopen/#findComment-1590772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.