php - Preg_replace_all links to markdown format -


we're converting markdown, before used 'in-house' system, both image links , data (e.g. alt) in bracket.

for example {image link}[optional alt other data]

now moving markdown, (our data stored markdown in database), need convert markdown:

so how can turn instances of {link}[optional data] (square brackets not required, {}) markdown equivalent:

basically,

{http://www.youtube.com/image.gif}[this optional alt] ![alt](http://www.youtube.com/image.gif)

i have following far, deal optional [alt data] tag?

   if (preg_match_all('/\[(.*?)\]/i', $string, $matches, preg_set_order))     {      } 

to deal optional alt attribute should use preg_replace_callback. allows test existence of alt attr , add if necessary.

$str = ' image {http://www.youtube.com/image.gif}[this optional alt] image alt attribute {http://www.youtube.com/image.gif} ';  echo preg_replace_callback(     '~{(http://[^s]+)}(?:\[(.*?)\])?~',     function($m){         if ( isset( $m[2] ) ) {             return $img = sprintf( '![%s](%s)', $m[2], $m[1] );         }         return $img = sprintf( '(%s)', $m[1] );     },     $str ); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -