开发者

Help With Make Html Function

this is my first function in PHP Here is the code

<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)开发者_如何学Python - $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>

it make html on first occurence of [p1]/[p1] but how to make for the second any help or idea ?


This is going to return the first code inside the first occurrence. Modify the array index of the $content array to get other ones.

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

Also to replace it with your html, replace the entire occurrence, starting from [p1] and ending with [/p1] with your html whatever function. To do that you will need preg_replace() and a regex (regular expression) which matches the everything inside including the [p1] tags. -->

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

The replacement code should look like this:

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 is a string with the code which you need to replace.

$content is an array with the code between the [p1] tags

I assume that the parameter to the makeHTML function should be the code between the [p1] tags.

That should do it. ;) Vote for my answer!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜