I’ve a text stored in a value, and I only want to get the text after “eindformaat”.
Here is the text:
$desc = 'Folder DIN A4 2-breuk altaarvouw
135g druk glanzend
4/4 kleurig (dubbelzijdige druk)
2-breuk altaarvouw
eindformaat: 29,7 cm x 41,9 cm
gevouwen eindformaat: 20,95 cm x 29,7 cm
gegevensformaat: 29,9 cm x 42,1 cm';
What I want is the value after “eindformaat”. So this will be: 29,7 cm x 41,9 cm in this case.
I tried a lot of patterns with preg_replace. But none of them works for me.
I tried this, but it didn’t work:
preg_replace('/\/eindformaat\/.*/', '', $desc);
Can somebody help me out? Thanks.
You don’t want
preg_replace, butpreg_matchfor extracting. And then just leave out the escaped\/slashes, as those are not present in the text.You need
(.*)in parens, so it becomes the result capture group[1]. Also.*is sufficient as it won’t match beyond the linebreak without regex/sflag.The actual advantage of a regex is that you can make it super-specific:
And as @Jpsy points out, if you really only want to match the first
eindformaat:, not the second with a preceding word, then add an^anchor and the/mregex flag, like so: