1

I have a WYSIWYG editor and my client wants to use a similiar pattern like Wordpress shortcodes.

Effectively the client wants to do something like:

 [.class_name]
      /// Some content here
 [/close_container]

I am able to easily replace [/close_container] by using str_replace(), but as class_name will change with each shortcode used (say it was [.green_block] I first somehow have to capture green_block and then replace the entire [.green_block] with <div class='green_block'>. There is no predefined list of classes so I am a little clueless as how to approach this.

Any ideas?

4
  • Even strpos can help you to find it, and substr_replace to replace Commented Apr 11, 2013 at 21:13
  • If there is no list of predefined classes, why don't you let enter just <div class="...">...</div> and done? Commented Apr 11, 2013 at 21:15
  • @M8R-1jmw5r - I tried my best to push for this but the client is reluctant to write any html code. Trust me I tried :/ Commented Apr 11, 2013 at 21:16
  • Ok, Can you ask them to enter: [class=.class_name] ? Commented Apr 11, 2013 at 21:17

3 Answers 3

1

This is exactly what regular expressions would be perfect for:

$string = preg_replace('/\[\.(\w+)\]/i', '<div class="$1">', $string);
$string = str_replace('[/close_container]', '</div>', $string);

Obviously the above wont have any error checking, so could quite easily construct malformed html. With some extra work however you could get around this issue and build something quite stable.

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

Comments

1

Use

$formated_string= preg_replace("/\[\.(\w\-\s)\]/", "$1", $entire_string);

Shalom...

Comments

1

With the limited set of allowed input (and making your client require to write them in HTML entities if not meant as special code character &#91; [ / &#93; ]:

strtr($string, array(
    '[/close_container]' => '</div>', 
    '[.' => '<div class="', 
    ']' => '">'
));

Works like a charm.

Comments

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.