Reputation: 17332
I need to remove some parts of a file:
The content can look like this:
Lorem ipsum 12, 14
Lorem 29
34
Lorem s. anything
This should become this one:
Lorem ipsum
Lorem
Lorem
1) All numbers should be removed (there can be multiple numbers separeted with a comma). If there is nothing else then the number, the line should be removed
2) If there is "s.", everything after this point in this line should be removed.
This is my attempt:
$myfile = fopen("file.txt", "r") or die("Unable to open file!");
while(!feof($myfile)) {
// remove some parts and output
$newString = preg_replace("/\d+$/","",fgets($myfile));
echo newString . "<br>";
}
fclose($myfile);
Upvotes: 3
Views: 1663
Reputation: 67968
\d+(\s*,\s*\d+)*|\bs\..*$
Try this.Replace by empty string
.See demo.
http://regex101.com/r/kP8uF5/10
$re = "/\\d+(\\s*,\\s*\\d+)*|\\bs\\..*$/im";
$str = "Lorem ipsum 12, 14\nLorem 29\n34\nLorem s. anything\nvcbvcbv";
$subst = "";
$result = preg_replace($re, $subst, $str);
Upvotes: 1
Reputation: 785058
Search for this regex:
( *\d+(, *\d+)*|s\..*)$
And reply by empty string.
You can also use this regex (thanks to @hwnd):
(?:[\d, ]+| *s\..*)$
Upvotes: 2