SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Thread: Regular expressions question
-
Jan 16, 2001, 14:09 #1
- Join Date
- Oct 2000
- Location
- Nashvegas Baby!
- Posts
- 7,845
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have a text file of email addresses. The lines look like this:
ivymoe@mindspring.com Email from plus one online Sun 12/31/00 6:22 PM
I would like to get rid of the part in bold, but I don't want to go through all 600 lines by hand. I know that it can be done with regex and indeed I have done lesser thing than this myself. I just can't figure out how to get this one set up.
Anyone have a solution?Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
-
Jan 16, 2001, 14:48 #2
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
which part is in bold? I would do it like this
$file = file("yourtextfile");
for($i=0;$i<count($file);$i++) {
$data[] = str_replace("texttoreplace", "", $file[$i]);
}
$fp = fopen("sametextfile", "w");
for($i=0;$i<count($data);$i++) {
fputs($fp, $data[$i]."\n");
}
fclose($fp);Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Jan 16, 2001, 14:55 #3
- Join Date
- Oct 2000
- Location
- Nashvegas Baby!
- Posts
- 7,845
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ivymoe@mindspring.com Email from plus one online Sun 12/31/00 6:22 PMAdobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
-
Jan 16, 2001, 15:02 #4
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$file = file("yourtextfile");
$fp = fopen("sametextfile", "w");
for($i=0;$i<count($file);$i++) {
fputs($fp, substr($file[$i], 0, strpos($file[$i], " "))."\n");
}
fclose($fp);
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Jan 16, 2001, 15:04 #5
- Join Date
- Oct 2000
- Location
- Nashvegas Baby!
- Posts
- 7,845
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I figured it out... I had to do it in like 4 steps, but I got it after all...
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
-
Jan 16, 2001, 15:09 #6
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Did you try it my way I am positive it is ten times faster than taking ten steps.
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Jan 16, 2001, 15:47 #7
- Join Date
- Oct 2000
- Location
- Nashvegas Baby!
- Posts
- 7,845
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am sure your way would be faster...especially if I was doing it more than once, but for this one time I didn't mind doing it the hard way. It still only took me about 3 minutes...so no big deal.
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
-
Jan 16, 2001, 16:53 #8
- Join Date
- Apr 2000
- Location
- Waco, Texas.
- Posts
- 188
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I know you have already done it, but I decided to write some code anyhow.
Code:<? $file = "C:\Windows\Desktop\untitled.txt"; $content = implode('',file($file)); $content = preg_replace("/([^\s]*)[^\n]*/","\\1",$content); $out = fopen($file, "w"); fputs($out,$content); fclose($out); print $content; ?>
str_replace("Email from plus one online Sun 12/31/00 6:22 PM","", $file[$i]);
would only affect one line most likely. Not sure is str_replace() would be the best function to use than, because I don't think it can handle complex string patterns? (I could always be wrong about that though)
-
Jan 16, 2001, 16:58 #9
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Guess you didn't see my second post?? Which would definetly be faster than imploding each line and using preg.
Please don't PM me with questions.
Use the forums, that is what they are here for.
Bookmarks