<?php
$string = "Boa __tarde__ **Bacco**, isto é um **teste** com diversos **negritos** e __sublinha_dos__
__**um** dois__ **__um__ dois**
__aqui nao_funciona __ __ nem_aqui,pois está errado__
__aqui está certo__ ___errado__ __ce rto__" ;
preg_match_all ( "/(.?)(\*\*([^\*\s]+\s*\*?)*[^\s\*]+\*\*)([^\*]|$)/" , $string , $resultNegrito ) ;
$negrito = $resultNegrito [ 2 ] ;
$iniNegrito = $resultNegrito [ 1 ] ; // valores do grupo (.?)
for ( $x = 0 ; $x < count ( $negrito ) ; $x ++ ) { if ( $iniNegrito [ $x ] != "*" ) {
$res = "<b>" . substr ( $negrito [ $x ] , 2 , strlen ( $negrito [ $x ] ) - 4 ) . "</b>" ; }
}
preg_match_all ( "/(.?)(__([^_\s]+\s*_?)*[^\s_]+__)([^_]|$)/" , $string , $resultSublinhado ) ;
$sublinhado = $resultSublinhado [ 2 ] ;
$iniSublinhado = $resultSublinhado [ 1 ] ; // valores do grupo (.?)
for ( $x = 0 ; $x < count ( $sublinhado ) ; $x ++ ) { if ( $iniSublinhado [ $x ] != "_" ) {
$res = "<u>" . substr ( $sublinhado [ $x ] , 2 , strlen ( $sublinhado [ $x ] ) - 4 ) . "</u>" ; }
}
echo $string ;
PD9waHAKCiAgICAgICAgJHN0cmluZyA9ICJCb2EgX190YXJkZV9fICoqQmFjY28qKiwgaXN0byDDqSB1bSAqKnRlc3RlKiogY29tIGRpdmVyc29zICoqbmVncml0b3MqKiBlIF9fc3VibGluaGFfZG9zX18KCgkJX18qKnVtKiogZG9pc19fICAqKl9fdW1fXyBkb2lzKioKCQlfX2FxdWkgbmFvX2Z1bmNpb25hIF9fIF9fIG5lbV9hcXVpLHBvaXMgZXN0w6EgZXJyYWRvX18KCQlfX2FxdWkgZXN0w6EgY2VydG9fXyBfX19lcnJhZG9fXyBfX2NlICBydG9fXyI7CgoJCXByZWdfbWF0Y2hfYWxsKCIvKC4/KShcKlwqKFteXCpcc10rXHMqXCo/KSpbXlxzXCpdK1w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