I have this data in a mysql field:
First text text text text
text text text text
text text text text text text
text text text text text text
Second text text text text
text text text text
text text text text text text
text text text text text text
I'm trying to preg match from the first word until the double line break. I've tried:
preg_match('/First(.*)
/', $mysqlrow, $m);
This returns no rows. I've also tried the m
and s
modifiers, which don't return the proper string. I've also tried which doesn't return anything.
How do you do this
You need the s
modifier and you also need to change your wildcard quantifier to be non-greedy:
preg_match('/First(.*?)
/s', $mysqlrow, $m);
The s
option will cause the .
wildcard to match and the non-greedy will cause the
*
wildcard to not eat up all the it runs across before matching
.
You can also optionally match the just in case you have that in your database:
preg_match('/First(.*?)?
?
/s', $mysqlrow, $m);
It's the other flag you need. Flag "s" allow the dot to capture line break. But it will then be eating your double ' '.
You can try :
preg_match('/First(.*?)
/s', $mysqlrow, $m);
with the ? inverting the greedy behaviour. Note that spliting a string can be done with the split function which might be more suitable for your case.