I want to get ${1} = Title, ${2} = Open, ${3} = Bla-bla-bla.
from
{{Title|Open
Bla-bla-bla
}}
$string = "{{Title|Open
Bla-bla-bla
}}";
preg_match('/^\{\{([^|]+)\|(.*?)[\r\n]+(.*?)\s*\}\}/', $string, $matches);
print_r($matches);
What about something like this :
$str = <<<STR
{{Title|Open
Bla-bla-bla
}}
STR;
$matches = array();
if (preg_match("/^\{\{([^\|]+)\|([^\n]+)(.*)\}\}$/s", $str, $matches)) {
var_dump($matches);
}
It'll get you :
array
0 => string '{{Title|Open
Bla-bla-bla
}}' (length=28)
1 => string 'Title' (length=5)
2 => string 'Open' (length=4)
3 => string '
Bla-bla-bla
' (length=14)
Which means that, after using trim
on $matches[1]
, $matches[2]
, and $matches[3]
, you'll get what you asked for :-)
Explaining the regex :
^
{
characters, that have to be escaped, as they have a special meaning [^\\|]+
()
so it's captured -- returned as the first part of the result |
has to be escaped too. |
character -- that has to be escaped. [^\\n]+
()
so it's captured too -- second part of the result .*
virtually "anything" anynumber of times
()
so it's captured too -- third part of the result }
(escaped, too) $
And note the regex has the s
(dotall) modifier ; see Pattern Modifiers , about that.
http://www.gskinner.com/RegExr/
a useful place to play around and learn regexes.
In Perl:
/\{\{ # literal opening braces
(.*?) # some characters except new line (lazy, i. e. as less as possible)
\| # literal pipe
(.*?) # same as 2 lines above
\n # new line
([\s\S]*?) # any character, including new line (lazy)
\}\}/x; # literal closing braces
Making a more precise solution depends on what exact rules you want for extraction of your fields.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.