reg.exp og explode i if løkke.
Hej E.har følgende kode:
else if(list($tag, $link) = explode(":", $val[1])) {
$val[1] = str_replace($val[1], $link, $val[1]);
$val[1] = str_replace("]", "", $val[1]);
$val[1] = "<a href=\"".$val[1]."\">";
$val[4] = str_replace($val[4], "</a>", $val[4]);
$finished_string = $val[1].$val[3].$val[4];
}
den tjekker noget tekst for bla. [url:minside.php]mit linknavn[/url]
problemet er nu at den laver alt under det til links.
Hele scriptet ser sådan her ud:
<?php
// The \\2 is an example of backreferencing. This tells pcre that
// it must match the second set of parentheses in the regular expression
// itself, which would be the ([\w]+) in this case. The extra backslash is
// required because the string is in double quotes.
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
$html2 = "bold text[url:howdy.html]click me[/url]
header1
Header2
Header3
";preg_match_all("/(\[([\w]+)[^\]]*\])(.*)(\[\/\\2\])/", $html2, $matches, PREG_SET_ORDER);
//preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
if($val[1] == "" AND $val[4] == "") {
$val[1] = str_replace("","<b>", $val[1]);
$val[4] = str_replace("","</b>", $val[4]);
$finished_string = $val[1].$val[3].$val[4];
}
else if(list($tag, $link) = explode(":", $val[1])) {
$val[1] = str_replace($val[1], $link, $val[1]);
$val[1] = str_replace("]", "", $val[1]);
$val[1] = "<a href=\"".$val[1]."\">";
$val[4] = str_replace($val[4], "</a>", $val[4]);
$finished_string = $val[1].$val[3].$val[4];
}
else if($val[1] == "
" AND $val[4] == "
") {$val[1] = str_replace("
","<h1>", $val[1]);
$val[4] = str_replace("
","</h1>", $val[4]); $finished_string = $val[1].$val[3].$val[4];
}
else {
$finished_string = "no formation";
}
echo "matched: " . $val[0] . "<br>\n";
echo "part 1: " . htmlentities($val[1]) . "<br>\n";
echo "part 2: " . htmlentities($val[3]) . "<br>\n";
echo "part 3: " . htmlentities($val[4]) . "<br>\n<br>\n";
echo "result: " . $finished_string."<br>\n<br>\n";
}
//}
?>
Nogle bud på hvad der kan være galt ?
