Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/04/2009, 10:27
davidnando
 
Fecha de Ingreso: abril-2009
Mensajes: 8
Antigüedad: 15 años, 7 meses
Puntos: 0
De javascript a php

DE JAVASCRIPT A PHP

Hola amigos, acabo de descubrir esto del php y ya tengo un auténtico problema que me tiene enganchado. El caso es que estoy intentando hacer una versión en php de un curioso javascript. Se trata de un generador de poemas aleatorio y lo podéis ver en www(punto)thinkzone.wlonk.com/PoemGen/PoemGen.htm
Os dejo aquí el código javascript extraído del html. Con mis escasas nociones de php y algunos manuales intenté hacer algo similar valiéndome de sencillos random, luego descubrí que no era posible de esa forma, además lo suyo sería que el resultado no saliera en una caja de texto sino que quedara integrado en el html y cambiara al recargar la página. ¿Podéis echarme una mano? Mi madre dice que si no salgo a tomar el aire me deja sin Internet! ;D

Un saludo

<script type="text/javascript">

random_count=0;
nsentences=0;
nwords1=0; nwords2=0; nwords3=0; nwords4=0; nwords5=0;
nwords6=0; nwords7=0; nwords8=0; nwords9=0;

function init(form)
{
form.Samples.selectedIndex=1;
generate_input(form, form.Samples.options[form.Samples.selectedIndex].text);
make_poem(form);
}

function generate_input(form, sample_name)
{
form.title1.value = "Concrete Nouns";
form.title2.value = "Abstract Nouns";
form.title3.value = "Transitive Verbs";
form.title4.value = "Intransitive Verbs";
form.title5.value = "Adjectives";
form.title6.value = "Adverbs";
form.title7.value = "";
form.title8.value = "";
form.title9.value = "Interjections";
form.sentences.value =
"THE 5 1 6 3S THE 1."
+"\n5, 5 1S 6 3 A 5, 5 1."
+"\n2 IS A 5 1."
+"\n9, 2!"
+"\n1S 4!"
+"\nTHE 1 4S LIKE A 5 1."
+"\n1S 4 LIKE 5 1S."
+"\nWHY DOES THE 1 4?"
+"\n4 6 LIKE A 5 1."
+"\n2, 2, AND 2."
+"\nWHERE IS THE 5 1?"
+"\nALL 1S 3 5, 5 1S."
+"\nNEVER 3 A 1."
;
if (sample_name == "Sea") {
form.list1.value = "SEA\nSHIP\nSAIL\nWIND\nBREEZE\nWAVE\nCLOUD\nMAST\ nCAPTAIN\nSAILOR\nSHARK\nWHALE\nTUNA\nSEASHELL\nPI RATE\nLAD\nGIRL\nGULL\nREEF\nSHORE\nMAINLAND\nMOON \nSUN";
form.list2.value = "ADVENTURE\nCOURAGE\nENDURANCE\nDESOLATION\nDEATH\ nLIFE\nLOVE\nFAITH";
form.list3.value = "COMMAND\nVIEW\nLEAD\nPULL\nLOVE\nDESIRE\nFIGH T";
form.list4.value = "TRAVEL\nSAIL\nWAVE\nGROW\nRISE\nFALL\nENDURE\nDIE ";
form.list5.value = "BIG\nSMALL\nOLD\nCOLD\nWARM\nSUNNY\nRAINY\nMISTY\ nCLEAR\nSTORMY\nROUGH\nLIVELY\nDEAD";
form.list6.value = "SWIFTLY\nCALMLY\nQUIETLY\nROUGHLY";
form.list7.value = "";
form.list8.value = "";
form.list9.value = "O\nOH\nOOH\nAH\nLORD\nGOD\nWOW\nGOLLY GOSH";
} else if (sample_name == "City") {
form.list1.value = "CITY\nSTREET\nSIDEWALK\nCORNER\nDOOR\nWINDOW\nCAR \nTRUCK\nGUY\nGIRL\nJOB\nFLOWER\nLIGHT\nCIGARETTE\ nRAIN\nSKYSCRAPER\nJACKHAMMER\nDRIVER\nWORKER";
form.list2.value = "ACTION\nWORK\nNOISE\nDESOLATION\nDEATH\nLIFE\nLOV E\nFAITH\nANGER\nEXHAUSTION";
form.list3.value = "GET\nGRAB\nPUSH\nLOVE\nDESIRE\nBUY\nSELL\nFIGHT\n HUSTLE";
form.list4.value = "TALK\nWALK\nGOSSIP\nRUN\nGO\nSTOP\nEAT\nGROW\nSHR INK\nSHOP\nWORK";
form.list5.value = "BIG\nSMALL\nOLD\nFAST\nCOLD\nHOT\nDARK\nDUSTY\nGR IMY\nDRY\nRAINY\nMISTY\nNOISY\nFACELESS\nDEAD";
form.list6.value = "QUICKLY\nLOUDLY\nCALMLY\nQUIETLY\nROUGHLY";
form.list7.value = "";
form.list8.value = "";
form.list9.value = "O\nOH\nOOH\nAH\nLORD\nGOD\nDAMN";
} else {
clear_all(form);
}
form.outtext.value = "";
count_all_lines(form);
}

function clear_all(form)
{
form.Samples.value = "-";
form.title1.value = "";
form.title2.value = "";
form.title3.value = "";
form.title4.value = "";
form.title5.value = "";
form.title6.value = "";
form.list1.value = "";
form.list2.value = "";
form.list3.value = "";
form.list4.value = "";
form.list5.value = "";
form.list6.value = "";
form.list7.value = "";
form.list8.value = "";
form.list9.value = "";
form.sentences.value = "";
form.outtext.value = "";
count_all_lines(form);
}

function count_all_lines(form)
{
nwords1 = count_lines(form.list1);
nwords2 = count_lines(form.list2);
nwords3 = count_lines(form.list3);
nwords4 = count_lines(form.list4);
nwords5 = count_lines(form.list5);
nwords6 = count_lines(form.list6);
nwords7 = count_lines(form.list7);
nwords8 = count_lines(form.list8);
nwords9 = count_lines(form.list9);
nsentences = count_lines(form.sentences);
}

function random(maxnum)
{
r = Math.floor(Math.random() * maxnum) + 1;
if (r > maxnum) r = maxnum;
return r;
}

function count_lines(txt)
{
str = txt.value;
len = str.length;
nword = 1;
for (i = 0; i < len; i++) {
if (str.charAt(i) == "\n") {
nword++;
}
}
if (str.charAt(len-1) == "\n") nword--;
return nword;
}

function get_line(str, lnum)
{
len = str.length;
iline = 1; ichar = 0; jchar = -1;
for (i = 0; i < len; i++) {
if (str.charAt(i) == "\n") {
iline++;
if (iline == lnum) {
ichar = i + 1;
} else if (iline == (lnum + 1)) {
jchar = i - 1;
if (str.charAt(jchar) == "\r") {
jchar--;
}
break;
}
}
}
if (jchar < 0) jchar = len - 1;
// Note: substring() doesn't work consistently on all browsers
s = "";
for (i = ichar; i <= jchar; i++) {
s = s + str.charAt(i);
}
return s;
}

function make_poem(form)
{
form.outtext.value = "";
count_all_lines(form);
nlines = random(4) + 1;
for (ilin = 1; ilin <= nlines; ilin++) {
make_poem_line(form)
}
}

function make_poem_line(form)
{
pattern = get_line(form.sentences.value, random(nsentences));
lenpat = pattern.length;
for (ichr = 0; ichr < lenpat; ichr++) {
chr = pattern.charAt(ichr);
// If the pattern contains a digit n, then pick a random word from list n
if ((chr >= '1') && (chr <= '9')) {
if (chr == '1') {
wrd = get_line(form.list1.value, random(nwords1));
} else if (chr == '2') {
wrd = get_line(form.list2.value, random(nwords2));
} else if (chr == '3') {
wrd = get_line(form.list3.value, random(nwords3));
} else if (chr == '4') {
wrd = get_line(form.list4.value, random(nwords4));
} else if (chr == '5') {
wrd = get_line(form.list5.value, random(nwords5));
} else if (chr == '6') {
wrd = get_line(form.list6.value, random(nwords6));
} else if (chr == '7') {
wrd = get_line(form.list7.value, random(nwords7));
} else if (chr == '8') {
wrd = get_line(form.list8.value, random(nwords8));
} else if (chr == '9') {
wrd = get_line(form.list9.value, random(nwords9));
} else {
wrd = '';
}
form.outtext.value = form.outtext.value + wrd;
} else {
form.outtext.value = form.outtext.value + chr;
}
}
form.outtext.value = form.outtext.value + "\n";
}</script>