Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
[PHP] Find & Edit After [PHP]
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="NSA" data-source="post: 200442" data-attributes="member: 10034"><p>Hello,</p><p> </p><p>I'm trying to make a PHP installation file for one of my applications.</p><p>I have a text file like so:</p><p> </p><p>inc21 : 12</p><p>inc22 : 29</p><p>inc87 : 21</p><p> </p><p>I want PHP to search for the : and edit the "12" to a set string.</p><p>So if I have a string of text say "Hello", I want "12" to be replaced by "Hello".</p><p> </p><p>How does one accomplish this.</p><p> </p><p>I tried using this (see below) but it was returning the full line instead of just "12".</p><p> </p><p>[PHP]<?php</p><p>$file = 'somefile.txt';</p><p>$searchfor = ': ';</p><p> </p><p>// the following line prevents the browser from parsing this as HTML.</p><p>header('Content-Type: text/plain');</p><p> </p><p>// get the file contents, assuming the file to be readable (and exist)</p><p>$contents = file_get_contents($file);</p><p>// escape special characters in the query</p><p>$pattern = preg_quote($searchfor, '/');</p><p>// finalise the regular expression, matching the whole line</p><p>$pattern = "/^.*$pattern.*\$/m";</p><p>// search, and store all matching occurences in $matches</p><p>if(preg_match_all($pattern, $contents, $matches)){</p><p> echo "Found matches:\n";</p><p> echo implode("\n", $matches[0]);</p><p>}</p><p>else{</p><p> echo "No matches found";</p><p>}</p><p>?>[/PHP]</p></blockquote><p></p>
[QUOTE="NSA, post: 200442, member: 10034"] Hello, I'm trying to make a PHP installation file for one of my applications. I have a text file like so: inc21 : 12 inc22 : 29 inc87 : 21 I want PHP to search for the : and edit the "12" to a set string. So if I have a string of text say "Hello", I want "12" to be replaced by "Hello". How does one accomplish this. I tried using this (see below) but it was returning the full line instead of just "12". [PHP]<?php $file = 'somefile.txt'; $searchfor = ': '; // the following line prevents the browser from parsing this as HTML. header('Content-Type: text/plain'); // get the file contents, assuming the file to be readable (and exist) $contents = file_get_contents($file); // escape special characters in the query $pattern = preg_quote($searchfor, '/'); // finalise the regular expression, matching the whole line $pattern = "/^.*$pattern.*\$/m"; // search, and store all matching occurences in $matches if(preg_match_all($pattern, $contents, $matches)){ echo "Found matches:\n"; echo implode("\n", $matches[0]); } else{ echo "No matches found"; } ?>[/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
[PHP] Find & Edit After [PHP]
Top