newbtophp Posted November 2, 2009 Share Posted November 2, 2009 I have a large txt which looks like this: 7 blah...blahrandomtext ~7 ~, '%7C' 4 blah...blahrandomtext ~7 ~7, 'html' 5 blah...blahrandomtext ~7 ~7, '+' 6 blah...blahrandomtext ~7 ~7, 'xmlns' 7 blah...blahrandomtext ~7 ~7, '%7D' 8 blah...blahrandomtext ~7 ~7, '%22' 9 blah...blahrandomtext ~7 ~7, 'http' 10 blah...blahrandomtext ~7 ~7, '%7A%2F%2F' 11 blah...blahrandomtext ~7 ~7, 'www' 12 blah...blahrandomtext ~7 ~7, '.' 17 blah...blahrandomtext ~7 ~7, 'w7' 14 blah...blahrandomtext ~7 ~7, '.' 15 blah...blahrandomtext ~7 ~7, 'org' 16 blah...blahrandomtext ~7 ~7, '%2F' 17 blah...blahrandomtext ~7 ~7, '1999' 18 blah...blahrandomtext ~7 ~7, '%2F' 19 blah...blahrandomtext ~7 ~7, 'xhtml' 20 blah...blahrandomtext ~7 ~7, '%22' etc. I want to rearrange it using php tolook like this: %Chtml+xmlns%7D%22http%7A%2F%2Fwww etc. To remove everything except the stuff within the single quotes and join them? All help is greatly apreciated Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/ Share on other sites More sharing options...
ashton321 Posted November 2, 2009 Share Posted November 2, 2009 Not sure, but you may want to look into ltrim() Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/#findComment-949720 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 Not sure, but you may want to look into ltrim() That would be quite a trim.. you could get the string position of the first and last quote and use substring to pull it out, but should work (Untested) <?php $lines = file('theFile.txt'); $data = ""; foreach ($lines as $line) { if (preg_match('/\'([^\']*)\'/', $line, $regs)) { $data .= $regs[1]; } } echo $data; Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/#findComment-949723 Share on other sites More sharing options...
ashton321 Posted November 2, 2009 Share Posted November 2, 2009 Yeah it would be but im not all that great and it would be where i would start. Your way looks a lot better. Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/#findComment-949724 Share on other sites More sharing options...
newbtophp Posted November 2, 2009 Author Share Posted November 2, 2009 Thanks madtechie that worked Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/#findComment-949732 Share on other sites More sharing options...
salathe Posted November 2, 2009 Share Posted November 2, 2009 If it is a really large file, you won't be wanting to load it all into memory at once. How large is "large"? Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/#findComment-949735 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 Very true, if this is a one off then your be okay but for less memory usage try <?php $handle = fopen("theFile.txt", "r"); if ($handle) { $data = ""; while (!feof($handle)) { $line = fgets($handle, 4096); if (preg_match('/\'([^\']*)\'/', $line, $regs)) { $data .= $regs[1]; } } fclose($handle); echo $data; } Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/#findComment-949756 Share on other sites More sharing options...
thebadbad Posted November 2, 2009 Share Posted November 2, 2009 If the syntax is strictly as in your sample (i.e. with no single quotes in the random text), you could also use a single preg_match_all() call: <?php $source = file_get_contents('filename.txt'); preg_match_all('~\'([^\']+)\'~', $source, $matches); $data = implode('', $matches[1]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/#findComment-949770 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 That approach only works by loading everything into memory.. as salathe stated it could be a very large file, also this has been solved ! Quote Link to comment https://forums.phpfreaks.com/topic/180016-solved-rearranging-txt/#findComment-949778 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.