Html custom markup notations using simple php -
input - well, **mr. anderson** *_tall_* guy.
output - well, <strong>mr. anderson</strong> <em>_tall_</em> guy.
expected output - well, <strong>mr. anderson</strong> <em><span class="underlined">tall</span></em> guy.
i able make notation markup system not doing job when notations combined . can me please ? similar stackoverflow
's qa markup system
has better approach. , don't use plugins
$str = "well, **mr. anderson** *_tall_* guy."; $str_ar = explode(" ",$str); $new_str = ""; foreach($str_ar $s){ $new_str .= doreplace($s); } echo '<br />'. $new_str; function doreplace($str){ $new_str = ""; $new_str .= docheckfb($str); //$new_str .= checkfb($str,'back'); $new_str = " ".$new_str." "; return $new_str; } function docheckfb($str){ $new_str = checkfb($str,'front'); $new_str = checkfb($new_str,'back'); return $new_str; } function checkfb($str,$pos){ $new_str =""; if($pos == 'front'){ $switchcase = substr($str,0,1); }else if($pos == 'back'){ $switchcase = substr($str,strlen($str) - 1,1); } switch($switchcase){ case '*': if($pos == 'front'){ $subswitch = substr($str,1,1); }else if($pos == 'back'){ $subswitch = substr($str,strlen($str) - 2,1); } if($subswitch == '*'){ $globalassoc = "**"; $notationf = "<strong>"; $notationb = "</strong>"; $length = 2; }else{ $globalassoc = "*"; $notationf = "<em>"; $notationb = "</em>"; $length = 1; } break; case '_': $globalassoc = "*"; $notationf = "<span class=\"underlined\">"; $notationb = "</span>"; $length = 1; break; default : $new_str .= $str; return $new_str; } if($pos=='front'){ $new_str .= getreplaceddone($str,$globalassoc,$notationf,$length,"front"); }else if($pos == 'back'){ $new_str .= getreplaceddone($str,$globalassoc,$notationb,$length,"back"); } return $new_str; } function getreplaceddone($str,$globalassoc,$notation,$length,$pos){ $len = strlen($str); $new_str = ""; if($pos=='front'){ $tmp_str = substr($str,$length); $new_str .= $notation. $tmp_str; $new_str = checkfb($new_str,'back'); }else if($pos == 'back'){ $tmp_str = substr($str,0,$len - $length); $new_str .= $tmp_str.$notation; } return $new_str; }
Comments
Post a Comment