帮助制作Html函数


Help With Make Html Function

这是我在PHP中的第一个函数这里是代码

<html><head>
<style type="text/css">
.tright 
{
float:right;
margin:5px;
}
.tleft 
{
float:left;
margin:5px;
}
</style>
 </head><body>
<?php
$fullstring = '[p1]{{15em}}((tleft))
''Biography//
[[Name==My Name]]
[[Fathers Name==My Father Name]]
[[Mothers Name==My Mother Name]]
[[Date Of Birth==My Date Of Birth]]
[/p1][p1]{{15em}}((tright))
''Biography//
[[Name==My Name]]
[[Fathers Name==My Father Name]]
[[Mothers Name==My Mother Name]]
[[Date Of Birth==My Date Of Birth]]
[/p1]';

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr( $string,$ini,$len);
}
function Make_html($texttoprase){
//$parsed = get_string_between($texttoprase, "[p1]", "[/p1]");
$mywidth = get_string_between($texttoprase, "{{", "}}");
//replacing width variables
$texttoprase = str_replace("{{","",$texttoprase);
$texttoprase = str_replace("}}","",$texttoprase);
$texttoprase = str_replace($mywidth,"",$texttoprase);
//getiing class for table
$myclass = get_string_between($texttoprase, "((", "))");
//replacing class variables
$texttoprase = str_replace("((","",$texttoprase);
$texttoprase = str_replace("))","",$texttoprase);
$texttoprase = str_replace($myclass,"",$texttoprase);
$texttoprase = str_replace("[[","<tr><th scope='"row'" style='"text-align:left;'">",$texttoprase);
$texttoprase = str_replace("==","</th><td>",$texttoprase);
$texttoprase = str_replace("]]","</td></tr>",$texttoprase);
$texttoprase = str_replace("''","<tr><td colspan='"2'" style='"text-align:center;'">",$texttoprase);
$texttoprase = str_replace("//","</td></tr>",$texttoprase);
return "<div class='"" . $myclass . "'"><table style='"width: " . $mywidth . ";'" cellspacing='"5'"><tbody>" . $texttoprase . "</tbody></table></div>";
}
$texttofind = get_string_between($fullstring, "[p1]", "[/p1]");
$textToReplace =  Make_html($texttofind) ;
$fullstring = str_replace("[p1]" . $texttofind . "[/p1]", $textToReplace ,$fullstring);
echo $fullstring;
?></body></html>

它使HTML在第一次出现[p1]/[p1],但如何使第二次有任何帮助或想法?

这将返回第一个事件中的第一个代码。修改$content数组的数组索引以获取其他数组。

preg_match("|[p1](.+)[/p1]|si", $original_code, $content); 
echo $content[1]; 

也要用html替换它,替换整个事件,从[p1]开始,以[/p1]结束,用html中的任何函数。要做到这一点,您需要preg_replace()和一个regex(正则表达式),它匹配里面的所有内容,包括[p1]标记。——>

preg_replace('#([[p1])(.*)(['p1]])#', makeHTML(), $original_code, $limit_changes);

替换代码应该像这样:

preg_match("|[p1](.+)[/p1]|si", $original_code, $content); 
for( $i=1; $i<=count($content); $i++){
    echo $content[$i]; //This is the code between the tags
    preg_replace('#([[p1])(.*)(['p1]])#', makeHTML($content[$i]), $original_code, 1);
}

$original_code是一个包含你需要替换的代码的字符串。

$content是一个数组,其代码位于[p1]标签

之间我假设makeHTML函数的参数应该是[p1]标记之间的代码。

应该可以了。,)投票给我的答案吧!