atl_andy Posted March 25, 2008 Share Posted March 25, 2008 Taking a shot that this will be solved. The following will add (' to the first word in $file. I need to be able to add ') to the end as well, so ABC will become ('ABC'). However, I can't get it to add (' to all the words. I tried using g to globally add, but it didn't work. (user error of course) My thought is to send the results of $str through another for loop to add the ') to the end. But I'm lost. Any ideas would be appreciated. <?php $file = ("/var/www/html/string.php"); $lines = file_get_contents($file); for ($i=0; $i < count($lines); $i++) { $str = preg_replace("/^/","(' ",$lines); //I added some spaces for easier reading. echo "$str\n"; } Quote Link to comment Share on other sites More sharing options...
atl_andy Posted March 25, 2008 Author Share Posted March 25, 2008 Update: I added m in the regex and it add (' to the beginning of each word in the file. Now to add it to the end and write the results to a file.... <?php $file = ("/var/www/html/string.php"); $lines = file_get_contents($file); for ($i=0; $i < count($lines); $i++) { $str = preg_replace("/^/m","(' ",$lines); //I added the m. echo "$str\n"; } Quote Link to comment Share on other sites More sharing options...
atl_andy Posted March 25, 2008 Author Share Posted March 25, 2008 Update #2: <?php $file = ("/var/www/html/string.php"); $lines = file_get_contents($file); for ($i=0; $i < count($lines); $i++) { $str = preg_replace("/^/m","(' ",$lines); } for ($i=0; $i < count($lines); $i++) { $sta = preg_replace("/$/m"," ') ",$str); echo "$str\n"; } Quote Link to comment Share on other sites More sharing options...
effigy Posted March 25, 2008 Share Posted March 25, 2008 echo $lines = preg_replace('/([a-z]+)/i', "('$1')", $lines); Quote Link to comment Share on other sites More sharing options...
atl_andy Posted March 25, 2008 Author Share Posted March 25, 2008 That's nice. I had to modify a bit because some items are alpha-numeric with some dashes. The end result was: echo $lines = preg_replace('/([a-z0-9-]+)/i', "('$1');", $lines); And it worked perfectly. Thanks for your help! I'm using it to parse text files so I can import into sql. One other question....the results are shown in my broswer like: result result result result result result result result but I need: result result result Any suggestions? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 25, 2008 Share Posted March 25, 2008 Always place the hyphen first in a character class since it's a metacharacter. You can also add a new line after each word: echo $lines = preg_replace('/([-a-z\d]+)/i', "('$1')\n", $lines); Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 Either that, or escape the hyphen using \- Quote Link to comment Share on other sites More sharing options...
atl_andy Posted March 25, 2008 Author Share Posted March 25, 2008 Had to escape because there are a few other characters I had to add. The new line piece still isn't working...no biggie. Everything else has saved me many hours of c&p. Thanks again! Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 Newlines can also be \r\n (on windows-based machines. I usually check for newlines using (?:\r\n|\n|\r) Quote Link to comment Share on other sites More sharing options...
effigy Posted March 25, 2008 Share Posted March 25, 2008 The new line piece still isn't working...no biggie. Have you viewed the source? Quote Link to comment Share on other sites More sharing options...
atl_andy Posted March 25, 2008 Author Share Posted March 25, 2008 It's not there since I'm using only PHP, the only thing that shows is the results of the replace. I think you'll tell me something should be there? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 25, 2008 Share Posted March 25, 2008 If you're not using the CLI and checking results through a browser, you won't see line breaks because they don't honor them (unless you use the pre tag). You can, however, see them in the source. Quote Link to comment Share on other sites More sharing options...
atl_andy Posted March 25, 2008 Author Share Posted March 25, 2008 The pre tags did it. It's been a while since I used them. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 perhaps you wanted nl2br()? Quote Link to comment 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.