开发者

PHP REGEX: space & line break in paramters (custom tags)

this is my custom tag

[extract=A:B(

   <div>
       <p>Some content...</p>
   </div>

)]
  • The word extract stays as it is.
  • Value A has a string input (one word no spaces, no line breaks)
  • Value B will contain html closed in (). it will contain line breaks

I am not good with regular expressions but this is basically what I want.

\/[extract=(.*?):(.*?)/]\

I need the appropriate pattern query and a foreach loop, preg_match_开发者_如何学Call(), to return A & B


Try this:

preg_match_all('/\[extract=(?<class>\w+):(?<method>\w+)\((?<html>.*?)\)\]/s', $content, $matches);
print_r($matches['class']);
print_r($matches['method']);
print_r($matches['html']);

Should output:

Array
(
    [0] => A
)
Array
(
    [0] => B
)
Array
(
    [0] => 

   <div>
       <p>Some content...</p>
   </div>

)


This may not be perfect, but seems to work in very quick and limited testing. If nothing else, it might help you get to a better solution.

/^(?:\[extract=)(\w)+:(\w+\(.*\))\]$/s

Note that the trailing s flag is used to make the dot match all characters including new lines.


I don't fully understand your question. Where should the string input for "A" be? In the place of letter "A"?

If this is what you want, then the solution is:

preg_match_all('/\[extract=([^\s]+?):(.+)\]/s', 'your custom tag', $result);

So what you might be looking for is the modifier s which modifies the dot (".") character's meaning so to include linebreaks.

I also recommend you http://www.regular-expressions.info/ if you want to get more familiar with regular expressions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜