[PHP] MouseOvers toevoegen met PHP

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
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)));

?>

[ Voor 36% gewijzigd door Verwijderd op 17-10-2003 11:39 ]


Acties:
  • 0 Henk 'm!

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert

Laat me je een tip geven: doe dit niet met PHP maar met JavaScript:

JavaScript:
1
2
3
4
var td_collection = document.getElementsByTagName ( 'td' );
for ( var i = 0; i < td_collection.length; i ++ ) {
   td_collection.item(i).onmouseover='handler(...)';
}


eventueel kun je, om de html te produceren, de DOM tree daarnaa gewoon recursief doorlopen om de html te produceren inclusief de mouseovers.

En als je toch perse met PHP wilt werken moet je een preg_replace () doen en even uitvogelen hoe je regex in elkaar moet zitten.

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


Acties:
  • 0 Henk 'm!

Verwijderd

Gebruik idd Java zoals hierboven gezegt word en houd voortaan even rekening met 'Check Hier Is Me Source' posts.

Post je wel source... doe er dat even in een mooi [code] boxxie, zien we ook een een ge-tapte source.
1. Eindeloze lus
Welke lus?

Acties:
  • 0 Henk 'm!

Verwijderd

Is dit niet handig op te lossen met (goed, ingewikkelde..) rexuliere expressies?! Dus met de functie preg_replace oid?
Snap eigenlijk niet zo goed waarom je dit wilt doen, maar zal wel met je onderzoek te maken hebben.

/me heeft het gevoel dat hij zijn mond moet houden in dit topic :X

[ Voor 14% gewijzigd door Verwijderd op 17-10-2003 13:40 ]


Acties:
  • 0 Henk 'm!

  • RM-rf
  • Registratie: September 2000
  • Nu online

RM-rf

1 2 3 4 5 7 6 8 9

ook in php kun je DOM-methods toepassen
http://de3.php.net/manual/en/ref.domxml.php
enkel moet je input daarvoor valide XML zijn, en dus in dit geval XHTML.

er bestaan echter wel tidy-code-library's, die van non-valide HTML alsnog een clean-up maken, veel overbodige troep er sowieso uithalen e.d. dit kan echter wel tot kleine afwijkingen leiden.

Zoiets lijkt me een beter idee dan je eigen tag-parser te gaan schrijven

Intelligente mensen zoeken in tijden van crisis naar oplossingen, Idioten zoeken dan schuldigen


Acties:
  • 0 Henk 'm!

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert

de DOM methoden van PHP heb ik niet echt geweldige ervaringen mee, maar het kan inderdaad een oplossing zijn.

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz

Pagina: 1