Ik ben bezig voor mijn onderzoek een script te schrijven, dat een MouseOver toevoegt aan elke alinea. In HTML kan een alinea zijn:
1. Wat tussen p en /p staat
2. Wat tussen div en /div staat
3. Wat voor br komt
4. Wat tussen li en /li staat
5. Wat tussen td en /td staat
Extra probleem: wat tussen td en /td staat kan ook weer alinea's bevatten
Elke mouseover moet een uniek nummer hebben, zodat het kan worden opgeslagen in een db. Dus alinea 1 Handler (1), alinea 2 Handler (2), etc.
Ik heb hiervoor een script geschreven, maar kom er niet uit. Diverse mensen al gevraagd en in een ander forum gepost, zonder resultaat. Wie kan me helpen?
Het script staat hieronder. Problemen:
1. Eindeloze lus
2. Passages worden 2x of helemaal niet verwerkt.
Iemand een idee wat hier fout zit?
Alvast ontzettend bedankt!
Mark
<?php
//Tags that mark paragraph beginnings
$ndls[0]="<br";
$ndls[1]="<p";
$ndls[2]="<td";
$ndls[3]="<div";
$ndls[4]="";
function stripos($haystack, $needle, $offset=0) {
//Case insensitive version of strpos; not present in PHP version installed here
return strpos(strtoupper($haystack), strtoupper($needle), $offset);
}
function strirpos($haystack, $needle, $start = 0){
//Case insensitive version of strrpos; not present in PHP version installed here
$tempPos = stripos($haystack, $needle, $start);
if($tempPos === false){
if($start == 0){
//Needle not in string at all
return false;
}else{
//No more occurances found
return $start - strlen($needle);
}
}else{
//Find the next occurance
return strirpos($haystack, $needle, $tempPos + strlen($needle));
}
}
function MultiplePos ($haystack, $needles, $offset=0) {
//Returns the needle and its position from the first time it has been encountered in haystack
//Avoid the problem of interpreting 0 as not found or needle found at pos 0
$haystack=" ".$haystack;
$needle='';
$min=strlen($haystack);
for ($i=0;$i<sizeof($needles);$i++) {
//Test whether needle occurs in haystack and whether its pos is closer to offset than min
if (stripos($haystack, $needles[$i], $offset)>=1 && stripos($haystack, $needles[$i], $offset)<$min) {
$min=stripos($haystack, $needles[$i], $offset);
$needle=$needles[$i];
$ndllength=stripos($haystack, ">", $min) - $min;
}
}
$result['Needle']=$needle;
//Substract 1 from pos, to go back to original positions
$result['Position']=$min -1;
$result['Length']=$ndllength;
return($result);
}
function MultipleLastPos ($haystack, $closeneedles, $stop, $offset=0) {
//Returns the needle and its position from the last time it has been encountered in haystack
//Avoid the problem of interpreting 0 as not found versus needle found at pos 0
$haystack=" ".substr($haystack, $offset, $stop);
$needle='';
$max=0;
for ($i=0;$i<sizeof($closeneedles);$i++) {
//Test whether needle occurs in haystack and whether its pos is closer to offset than min
$curlastpos=strirpos($haystack, $closeneedles[$i], $offset) + strlen($closeneedles[$i]);
if ($curlastpos>$max) {
$max=$curlastpos;
$needle=$closeneedles[$i];
}
}
$result['Needle']=$needle;
//Substract 1 from pos, to go back to original positions
$result['Position']=$max -1;
return($result);
}
function tagContents ($hay, $tag, $start) {
//Returns what is between start tag and close tag, including the tag markers themselves
//$start => position of open tag
//$haystack => entire source text tag has to be derived from
$closetag="</".substr($tag, 1).">";
//Determine position of close tag
$endpos=stripos($hay, $closetag, $start);
//Determine and return cellcontents
$mytag['Content']=substr($hay, $start, ($endpos - $start + strlen($closetag)));
$mytag['Endpos']=$endpos + strlen ($closetag);
return($mytag);
}
function add_handler ($haystack) {
//Main function: inserts javascript mouse over handlers into every tag that marks the beginning of a paragraph,
//including table cells
$i=0;
//Tags that indicate a paragraph
$ndls[0]="<br";
$ndls[1]="<p";
$ndls[2]="<td";
$ndls[3]="<div";
while ($pos=MultiplePos ($haystack, $ndls, $i)) {
$tag=tagContents($haystack, $pos['Needle'], $i);
switch ($pos['Needle']) {
case "<p":
if (eregi("<p.* onmouseover=.*>", $tag['Content'])) {
$content_mod=eregi_replace("(<p )(.*)(onmouseover=\")(.*)", "\\1 onMouseOver=\"Handler(1); \\4", $tag['Content']);
}
else
{
$content_mod=eregi_replace("(<p)(.*>)", "\\1 onMouseOver=\"Handler()\" \\2", $tag['Content']);
}
$haystack=str_replace ($tag['Content'], $content_mod, $haystack);
echo("<FONT COLOR=\"red\">".htmlspecialchars($haystack)."</FONT><p>");
break;
case "<div":
//Inserts another mouseover if there is one already
if (eregi("<div.* onmouseover=.*>", $tag['Content'])) {
$content_mod=eregi_replace("(<div )(.*)(onmouseover=\")(.*)", "\\1 onMouseOver=\"Handler(1); \\4", $tag['Content']);
}
else
//Otherwise insert handler
{
$content_mod=eregi_replace("(<div)(.*>)", "\\1 onMouseOver=\"Handler()\" \\2", $tag['Content']);
}
//Replace old tag and its content in haystack with the new one
$haystack=str_replace ($tag['Content'], $content_mod, $haystack);
echo("<FONT COLOR=\"red\">".htmlspecialchars($haystack)."</FONT><p>");
break;
case "<br":
//If a <br> tag is found, the beginning of the corresponding paragraph is sought.
$closendls[0]="</p>";
$closendls[1]="</div>";
$closendls[2]="</td>";
//Returns the last closetag before the <br>
$lastclosetag = MultipleLastPos($haystack, $closendls, $pos['Position']);
//Replace <br> with </div>
$haystack = substr($haystack, 0, $pos['Position'])."</div>".substr($haystack, $pos['Position']+4);
//Store part before start of div
$beforelast = substr($haystack, 0, $lastclosetag['Position']);
//Store part after start of div
$afterlast = substr($haystack, $lastclosetag['Position']);
//Store both parts into haystack, including handler
$haystack = $beforelast."<div onMouseOver=\"Handler()\">".$afterlast;
echo("<FONT COLOR=\"red\">".htmlspecialchars($haystack)."</FONT><p>");
break;
case "<td":
//Retrieves content from what's enclosed by <td> en </td>
$length=stripos($haystack, "</td>", $i) - $pos['Position'] + strlen($pos['Needle']);
$cell=substr($haystack, $pos['Position'] +strlen($pos['Needle']), $length);
if (strlen($cell)==0) {
$cell=" ";
}
//Paragraphs in cell contents?
$process=false;
for ($i=0;$i<sizeof($ndls);$i++) {
if (stripos($cell, $ndls[$i]) && $ndls[$i]!="<td") { $process=true; }
}
if ($process==true) {
//Treat cell contents as if it were a new haystack: recursive
$cell=add_handler($cell);
$i = $i + length($cell) + 9;
}
elseif (eregi("<td.*onmouseover=.*>", $cell)) {
//Als cel geen alinea's bevat, maar wel een eerder mouse over, dan wordt deze toegevoegd
$cell_mod=eregi_replace("onMouseOver=\"", "onMouseOver=\"Handler(); ", $cell);
//Replace old cell with new cell
$haystack=eregi_replace("$cell","$cell_mod", $haystack);
}
else {
//Als cel noch alinea's bevat noch een andereer mouse over, dan wordt een mouse-over toegevoegd
$cell_mod=eregi_replace("<td", "<td onMouseOver=\"Handler()\"", $cell);
$haystack=eregi_replace("$cell","$cell_mod", $haystack);
}
break;
}
$i=$tag['Endpos'] + 1;
$pos='';
$tag='';
}
return($haystack);
}
$str="
<div>
asdjklsdfdsf
</div>
<p onmouseover=\"Test()\">Paragraaf met handler</p>
bla bal abla balasdlkfj als dd<br>
aa bb cc dd ee ff gg hh ii jj
<table>
<tr><td>Cel 1</td>
<td>Cel 2</td>
<td>Een paragraaf binnen een<br>cel 3</td>
<td></td>
</tr>
</table>";
echo (htmlspecialchars($add_handler($str)));
?>
1. Wat tussen p en /p staat
2. Wat tussen div en /div staat
3. Wat voor br komt
4. Wat tussen li en /li staat
5. Wat tussen td en /td staat
Extra probleem: wat tussen td en /td staat kan ook weer alinea's bevatten
Elke mouseover moet een uniek nummer hebben, zodat het kan worden opgeslagen in een db. Dus alinea 1 Handler (1), alinea 2 Handler (2), etc.
Ik heb hiervoor een script geschreven, maar kom er niet uit. Diverse mensen al gevraagd en in een ander forum gepost, zonder resultaat. Wie kan me helpen?
Het script staat hieronder. Problemen:
1. Eindeloze lus
2. Passages worden 2x of helemaal niet verwerkt.
Iemand een idee wat hier fout zit?
Alvast ontzettend bedankt!
Mark
<?php
//Tags that mark paragraph beginnings
$ndls[0]="<br";
$ndls[1]="<p";
$ndls[2]="<td";
$ndls[3]="<div";
$ndls[4]="";
function stripos($haystack, $needle, $offset=0) {
//Case insensitive version of strpos; not present in PHP version installed here
return strpos(strtoupper($haystack), strtoupper($needle), $offset);
}
function strirpos($haystack, $needle, $start = 0){
//Case insensitive version of strrpos; not present in PHP version installed here
$tempPos = stripos($haystack, $needle, $start);
if($tempPos === false){
if($start == 0){
//Needle not in string at all
return false;
}else{
//No more occurances found
return $start - strlen($needle);
}
}else{
//Find the next occurance
return strirpos($haystack, $needle, $tempPos + strlen($needle));
}
}
function MultiplePos ($haystack, $needles, $offset=0) {
//Returns the needle and its position from the first time it has been encountered in haystack
//Avoid the problem of interpreting 0 as not found or needle found at pos 0
$haystack=" ".$haystack;
$needle='';
$min=strlen($haystack);
for ($i=0;$i<sizeof($needles);$i++) {
//Test whether needle occurs in haystack and whether its pos is closer to offset than min
if (stripos($haystack, $needles[$i], $offset)>=1 && stripos($haystack, $needles[$i], $offset)<$min) {
$min=stripos($haystack, $needles[$i], $offset);
$needle=$needles[$i];
$ndllength=stripos($haystack, ">", $min) - $min;
}
}
$result['Needle']=$needle;
//Substract 1 from pos, to go back to original positions
$result['Position']=$min -1;
$result['Length']=$ndllength;
return($result);
}
function MultipleLastPos ($haystack, $closeneedles, $stop, $offset=0) {
//Returns the needle and its position from the last time it has been encountered in haystack
//Avoid the problem of interpreting 0 as not found versus needle found at pos 0
$haystack=" ".substr($haystack, $offset, $stop);
$needle='';
$max=0;
for ($i=0;$i<sizeof($closeneedles);$i++) {
//Test whether needle occurs in haystack and whether its pos is closer to offset than min
$curlastpos=strirpos($haystack, $closeneedles[$i], $offset) + strlen($closeneedles[$i]);
if ($curlastpos>$max) {
$max=$curlastpos;
$needle=$closeneedles[$i];
}
}
$result['Needle']=$needle;
//Substract 1 from pos, to go back to original positions
$result['Position']=$max -1;
return($result);
}
function tagContents ($hay, $tag, $start) {
//Returns what is between start tag and close tag, including the tag markers themselves
//$start => position of open tag
//$haystack => entire source text tag has to be derived from
$closetag="</".substr($tag, 1).">";
//Determine position of close tag
$endpos=stripos($hay, $closetag, $start);
//Determine and return cellcontents
$mytag['Content']=substr($hay, $start, ($endpos - $start + strlen($closetag)));
$mytag['Endpos']=$endpos + strlen ($closetag);
return($mytag);
}
function add_handler ($haystack) {
//Main function: inserts javascript mouse over handlers into every tag that marks the beginning of a paragraph,
//including table cells
$i=0;
//Tags that indicate a paragraph
$ndls[0]="<br";
$ndls[1]="<p";
$ndls[2]="<td";
$ndls[3]="<div";
while ($pos=MultiplePos ($haystack, $ndls, $i)) {
$tag=tagContents($haystack, $pos['Needle'], $i);
switch ($pos['Needle']) {
case "<p":
if (eregi("<p.* onmouseover=.*>", $tag['Content'])) {
$content_mod=eregi_replace("(<p )(.*)(onmouseover=\")(.*)", "\\1 onMouseOver=\"Handler(1); \\4", $tag['Content']);
}
else
{
$content_mod=eregi_replace("(<p)(.*>)", "\\1 onMouseOver=\"Handler()\" \\2", $tag['Content']);
}
$haystack=str_replace ($tag['Content'], $content_mod, $haystack);
echo("<FONT COLOR=\"red\">".htmlspecialchars($haystack)."</FONT><p>");
break;
case "<div":
//Inserts another mouseover if there is one already
if (eregi("<div.* onmouseover=.*>", $tag['Content'])) {
$content_mod=eregi_replace("(<div )(.*)(onmouseover=\")(.*)", "\\1 onMouseOver=\"Handler(1); \\4", $tag['Content']);
}
else
//Otherwise insert handler
{
$content_mod=eregi_replace("(<div)(.*>)", "\\1 onMouseOver=\"Handler()\" \\2", $tag['Content']);
}
//Replace old tag and its content in haystack with the new one
$haystack=str_replace ($tag['Content'], $content_mod, $haystack);
echo("<FONT COLOR=\"red\">".htmlspecialchars($haystack)."</FONT><p>");
break;
case "<br":
//If a <br> tag is found, the beginning of the corresponding paragraph is sought.
$closendls[0]="</p>";
$closendls[1]="</div>";
$closendls[2]="</td>";
//Returns the last closetag before the <br>
$lastclosetag = MultipleLastPos($haystack, $closendls, $pos['Position']);
//Replace <br> with </div>
$haystack = substr($haystack, 0, $pos['Position'])."</div>".substr($haystack, $pos['Position']+4);
//Store part before start of div
$beforelast = substr($haystack, 0, $lastclosetag['Position']);
//Store part after start of div
$afterlast = substr($haystack, $lastclosetag['Position']);
//Store both parts into haystack, including handler
$haystack = $beforelast."<div onMouseOver=\"Handler()\">".$afterlast;
echo("<FONT COLOR=\"red\">".htmlspecialchars($haystack)."</FONT><p>");
break;
case "<td":
//Retrieves content from what's enclosed by <td> en </td>
$length=stripos($haystack, "</td>", $i) - $pos['Position'] + strlen($pos['Needle']);
$cell=substr($haystack, $pos['Position'] +strlen($pos['Needle']), $length);
if (strlen($cell)==0) {
$cell=" ";
}
//Paragraphs in cell contents?
$process=false;
for ($i=0;$i<sizeof($ndls);$i++) {
if (stripos($cell, $ndls[$i]) && $ndls[$i]!="<td") { $process=true; }
}
if ($process==true) {
//Treat cell contents as if it were a new haystack: recursive
$cell=add_handler($cell);
$i = $i + length($cell) + 9;
}
elseif (eregi("<td.*onmouseover=.*>", $cell)) {
//Als cel geen alinea's bevat, maar wel een eerder mouse over, dan wordt deze toegevoegd
$cell_mod=eregi_replace("onMouseOver=\"", "onMouseOver=\"Handler(); ", $cell);
//Replace old cell with new cell
$haystack=eregi_replace("$cell","$cell_mod", $haystack);
}
else {
//Als cel noch alinea's bevat noch een andereer mouse over, dan wordt een mouse-over toegevoegd
$cell_mod=eregi_replace("<td", "<td onMouseOver=\"Handler()\"", $cell);
$haystack=eregi_replace("$cell","$cell_mod", $haystack);
}
break;
}
$i=$tag['Endpos'] + 1;
$pos='';
$tag='';
}
return($haystack);
}
$str="
<div>
asdjklsdfdsf
</div>
<p onmouseover=\"Test()\">Paragraaf met handler</p>
bla bal abla balasdlkfj als dd<br>
aa bb cc dd ee ff gg hh ii jj
<table>
<tr><td>Cel 1</td>
<td>Cel 2</td>
<td>Een paragraaf binnen een<br>cel 3</td>
<td></td>
</tr>
</table>";
echo (htmlspecialchars($add_handler($str)));
?>
[ Voor 36% gewijzigd door Verwijderd op 17-10-2003 11:39 ]