John_A Posted March 30, 2017 Share Posted March 30, 2017 (edited) I have a file in the same location as my PHP file, say test.txt, and in multiple locations it has content like: - href="http://www.domain.com/entry.php/256/1/this-is-the-title" or href="http://www.domain.com/entry.php/123/2/another-title" which I need replaced within the file to: - href="{EntryNumber=256.link}" and href="{EntryNumber=123.link}" Only the first number is needed for the replacement, it will be between 1 and 999. Everything between the 5th / and the 2nd " can be safely ignored and removed. This is not the only content in the line, and sometimes there may be more than one occurrence per line. I've got this so far: - $buffer = ""; $fp = file($file); foreach($fp as $line){ $buffer .= preg_replace(***struggling with this bit***); } fclose($fp); echo $buffer; file_put_contents($file, $buffer); Am I along the right lines generally? And I have no idea what the preg_replace should be as my regex is a bit trial and error, more error in this case... As always, any help greatly appreciated! EDIT: If it matters, the largest file I need to edit is 39Kb, and includes 175 occurrences of the replacements. Edited March 30, 2017 by John_A Quote Link to comment https://forums.phpfreaks.com/topic/303580-regex-help-replacing-text-in-multiple-lines-of-text-file/ Share on other sites More sharing options...
requinix Posted March 30, 2017 Share Posted March 30, 2017 There's no need for file() - take the whole file into a string, run one regex to replace all instances, then send it back to the file. The regex you're looking for could be like #"http://www.domain.com/entry.php/(\d+)/[^"]+"#Replace that with "{EntryNumber=\1.link}"Use single-quoted strings for those so PHP itself doesn't try to interpret what's in them. Quote Link to comment https://forums.phpfreaks.com/topic/303580-regex-help-replacing-text-in-multiple-lines-of-text-file/#findComment-1544828 Share on other sites More sharing options...
Solution darklexus2k9 Posted April 6, 2017 Solution Share Posted April 6, 2017 (edited) i think what you want todo is really easy and can be done with one simple line of regex preg_replace("/href=\"http:\/\/www.domain.com\/entry.php\/(.*?)\/(.*?)\"/", "href="{EntryNumber=$01.link}"", $input_data); you can see this working at the following link : http://www.phpliveregex.com/p/jGr Edited April 6, 2017 by darklexus2k9 Quote Link to comment https://forums.phpfreaks.com/topic/303580-regex-help-replacing-text-in-multiple-lines-of-text-file/#findComment-1545104 Share on other sites More sharing options...
John_A Posted April 7, 2017 Author Share Posted April 7, 2017 Thank-you both! I went with option #2, as it seemed the most straightforward. Worked perfectly Quote Link to comment https://forums.phpfreaks.com/topic/303580-regex-help-replacing-text-in-multiple-lines-of-text-file/#findComment-1545145 Share on other sites More sharing options...
darklexus2k9 Posted April 7, 2017 Share Posted April 7, 2017 glad it helped if you need any more help feel free to send me a direct message Quote Link to comment https://forums.phpfreaks.com/topic/303580-regex-help-replacing-text-in-multiple-lines-of-text-file/#findComment-1545151 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.