INPUT - Well, **Mr. Anderson** is a *_tall_* guy.
输出 - Well, <strong>Mr. Anderson</strong> is a <em>_tall_</em> guy.
预期输出 - Well, <strong>Mr. Anderson</strong> is a <em><span class="underlined">tall</span></em> guy.
我能够创建一个符号标记系统,但是当符号组合时它不能完成工作。
有人可以帮我吗?
这与stackoverflow
&#39; s QA Markup system
非常相似
有没有人有更好的方法。我不想使用插件
$str = "Well, **Mr. Anderson** is a *_tall_* guy.";
$str_ar = explode(" ",$str);
$new_str = "";
foreach($str_ar as $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;
}