0

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.

3
  • 4
    And I am wondering why you are not using a DOM Parser to get these values Commented Sep 9, 2010 at 13:56
  • 1
    Like Gordon said, although DOM Parser may sound a bit overwhelming... it's really what you need to use, as any kind of reg-ex will be a great deal of work and performance will suffer. Commented Sep 9, 2010 at 13:59
  • @Frederico actually, a Regex will perform faster, and I'm sure someone will come up with one for this specific case. It's just that a DOM Parser is more reliable, more generic and doesnt have to be taught to vaguely understand HTML first. Commented Sep 9, 2010 at 14:14

1 Answer 1

4

You can try with preg_split()


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

The code


Resources :

On the same topic :

Sign up to request clarification or add additional context in comments.

1 Comment

You need to put the pattern in parentheses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.