PHP Replace Custom Tags With List Tags

I have a database table that will store a description of a product. I want the user to be able to insert custom “[FEATURES]” tags to be able to list all of the features of the product quickly or without having to use a WYSIWYG editor or having to write their own html. So as shown below, the user will be able to specify the opening features tag and closing features tag list like so :

[FEATURES]
Feature # 1 would go here!
Feature # 2 would go here!
Feature # 3 would go here!
Feature # 4 would go here!
Feature # 5 would go here!
[/FEATURES]

So as shown above this is how the user would specify the list of features, but I would want them output in an un-ordered list like below:

<ul>
<li>Feature # 1 would go here!</li>
<li>Feature # 2 would go here!</li>
<li>Feature # 3 would go here!</li>
<li>Feature # 4 would go here!</li>
<li>Feature # 5 would go here!</li>
</ul>

I was hoping I could use the simple str_replace function

 str_replace("\n", "<li>", $string)

but I’m having trouble trying to figure how how to properly do so.
Any help is greatly appreciated.

You will need:
preg_match_all
explode
implode
preg_replace

Thanks for your reply, but I’m thinking more like:

  1. preg_match_all() to capture the features between the two [FEATURES] tags.
  2. explode to capture the newlines and turn them into an array.
  3. Then a foreach loop to capture each feature from the array and wrap them with Li tags to echo them out as a string.

I think combining those 3 ingredients would be enough.

k, and how will you echo out the bits NOT inside your pseudotags, and do so in such a way that the features are in the right place?

my product is amazing!
[FEATURES]
magic
rainbow
unicorns
[/FEATURES]
check my tag for more MAGIK!

This dealing with parsing bbcode tags from two years ago might give you an idea as to what can be involved.

IIRC, the things that caused bumps were

- getting the regex to work with whitespace
- dealing with nesteds
- capturing what was needed
- getting the escapes right

i.e. the regex and when to change out of escaped whitespace.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.