News content blocks and custom Regular Expression
I need to create a little facility to create a news article to my editors by a php software. I have created a way to use a custom block and module to help the editors to include static or dynamic blocks and modules just including in the text:
Example:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in mollis libero. Phasellus sollicitudin ligula pharetra magna egestas mattis. {block name="ads1"} Aenean lacinia lacinia sapien nec fermentum. {module name="thename" class="Thelist" method="Show" parameter1="first parameter" parameter2="second parameter" parameter3="third parameter"} Donec ligula lectus, egestas et molestie ac, tristique id mi. Donec sed scelerisque leo.
I am using two regex:
$ex = preg_match_all("/{((?:module|block).*?)}/sm", $text, $matches);
preg开发者_运维问答_match_all ( '/^{module\s+|\G(name|class|method)=("[^"]+"|\'[^\']+\'|\S+)(?:\s+|(?=}$))/i', $module, $matches );
The first one get all the items in the news article. The second one splits every block and modules in an array.
The question: The second regex doesn't get all the parameters but just one! I need to get all the parameters that the editor writes in the content of the news article.
You are not searching for them.
^{module\s+|\G(name|class|method|parameters)=("[^"]+"|\'[^\']+\'|\S+)(?:\s+|(?=}$))
^
You search for the term parameters
followed by a =
, but this does not exist. You named them parameter1
and so on. So change to
^{module\s+|\G(name|class|method|parameter\d+)=("[^"]+"|\'[^\']+\'|\S+)(?:\s+|(?=}$))
should help you.
Maybe try to do it in 2 steps
1 to find all modules
preg_match_all( '(.*{module(.+)}.*)Ui', $module, $matches );
or
preg_match_all( '(.*{(module|block)(.+)}.*)Ui', $module, $matches);
2 to find parameters for each model/block
preg_match_all ( '(([a-zA-Z0-9]+)=\"(.+)\")Ui', $module, $matches );
精彩评论