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]

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( '', $m[2], $m[1] ); } return $img = sprintf( '(%s)', $m[1] ); }, $str );
Comments
Post a Comment