开发者

Regex split into array of pieces

开发者 https://www.devze.com 2023-01-15 05:38 出处:网络
I was wondering how could I split this string {{MENU}}<li><a href=\"{{LINK}}\" title=\"{{TITLE}}\"><span>{{TITLE}}</span></a></li>{{/MENU}}

I was wondering how could I split this string

{{MENU}}<li><a href="{{LINK}}" title="{{TITLE}}"><span>{{TITLE}}</span></a></li>{{/MENU}}

Into array of:

array(
    "<li><a href=\"",
    "{{LINK}}",
    "\" title=\"",
    "{{TITLE}}",
    "\"><span>",
    "{{TITLE}}",
    "</span></a></li>"
)

It also should work with more different formats like:

{{MENU}}<a href="{{LINK}}" title="{{TITLE}}">{{开发者_开发百科TITLE}}</a>{{/MENU}}
{{MENU}}<b><a href="{{LINK}}" title="{{TITLE}}">{{TITLE}}</a></b>{{/MENU}}

My problem is that I don't know how to write this complex regex yet.


You can try with preg_split()


$yourArray = preg_split("/({{\w+}})/", $yourText, -1, PREG_SPLIT_DELIM_CAPTURE);

The code


Resources :

  • php.net - preg_split()

On the same topic :

  • Preg_split() help
0

精彩评论

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