I am trying to remove a piece of string that begins between Int. and newline.
$string = preg_replace('/Int.[\s\S]+?\n/', '', $string);
Here is the source string:
My address
Int. blabla
blabla blabla
Is that possible?
I am trying to remove a piece of string that begins between Int. and newline.
$string = preg_replace('/Int.[\s\S]+?\n/', '', $string);
Here is the source string:
My address
Int. blabla
blabla blabla
Is that possible?
You can use the following solution:
$s = "My address\nInt. blabla\nblabla blabla";
$s = preg_replace('~Int\.\h*([^\r\n]+)[\r\n]*~', '', $s);
echo $s; // => My address\nblabla blabla
See IDEONE demo
The regex will match Int., then zero or more spaces (\h*, just to left-trim the result) and then will capture into Group 1 one or more characters other than line feed and carriage return symbols (([^\r\n]+))
[^\r\n] with + quantifier matches 1+ characters other than newline symbols.To remove your string, just use the following code:
$string = "My address\nInt. blabla\nblabla blabla";
$regex = '~ # delimiter
^Int\. # Looks for Int. at the beginning of a line
.* # everything else except a newline
~xm'; # free spacing mode and multiline
$replacement = '';
$string = preg_replace($regex, $replacement, $string);
You need the modifiers x to allow the comments and m to allow the caret (^) to match at any line.
If you want to remove the whole line (including a newline character, that is), change the regex to:
$regex = '~^Int\..*\R~m';
See a demo on ideone.com.