Forum Moderators: coopster

Message Too Old, No Replies

Help with a script to make a script

         

olidenia

7:30 pm on Feb 14, 2010 (gmt 0)

10+ Year Member



Hi, I'm making a tool to save me time making a script but just dont know where to start.

I have a file with a textarea form:

<form name="form" action="proces.php" method="POST">
<textarea name="txt" rows="20" cols="60"></textarea><br>
<input type="submit">
</form>


And in the textarea I paste some towns in Spain:

ABDET
ADSUBIA
AGOST
AGRES
AIGÜES
AL PATRO
ALBATERA
ALCALA DE LA JOVADA
ALCALALI
ALCOCER DE PLANES
ALCOLEJA
ALCOY/ALCOI
ALCUDIA
ALFAFARA
ALGARS
ALGODA-MATOLA
ALGORFA
ALGOROS

What I want is a script to ad at he begining of each town:

echo "obj.options[obj.options.length] = new Option('


And at the end of each town:

','1');\n";


so then I get that list of towns like follows:

echo "obj.options[obj.options.length] = new Option('ABDET','1');\n";
echo "obj.options[obj.options.length] = new Option('ADSUBIA','2');\n";
echo "obj.options[obj.options.length] = new Option('AGOST','3');\n";
echo "obj.options[obj.options.length] = new Option('AGRES','4');\n";
echo "obj.options[obj.options.length] = new Option('AIGÜES','5');\n";
echo "obj.options[obj.options.length] = new Option('AL PATRO','6');\n";
echo "obj.options[obj.options.length] = new Option('ALBATERA','7');\n";
echo "obj.options[obj.options.length] = new Option('ALCALA DE LA JOVADA','8');\n";
echo "obj.options[obj.options.length] = new Option('ALCALALI','9');\n";
echo "obj.options[obj.options.length] = new Option('ALCOCER DE PLANES','10');\n";
echo "obj.options[obj.options.length] = new Option('ALCOLEJA','11');\n";
echo "obj.options[obj.options.length] = new Option('ALCOY/ALCOI','12');\n";
echo "obj.options[obj.options.length] = new Option('ALCUDIA','13');\n";
echo "obj.options[obj.options.length] = new Option('ALFAFARA','14');\n";
echo "obj.options[obj.options.length] = new Option('ALGARS','15');\n";
echo "obj.options[obj.options.length] = new Option('ALGODA-MATOLA','16');\n";
echo "obj.options[obj.options.length] = new Option('ALGORFA','17');\n";
echo "obj.options[obj.options.length] = new Option('ALGOROS','18');\n";


How do I make the proces.php to do this?

Readie

3:14 am on Feb 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's quite simple really:
- You take the $_POST value of the form
- Then you replace new lines with some random stuff that you're never going to enter
- Then you explode it into an array with the aforementioned random stuff
- Count the number of pieces this creates
- Run a loop to get your result:

<?php

$input = $_POST['txt'];
$input = preg_replace('/[\r\n]{2}/m', '|##/|\#|#|', $input);
$input = explode("|##/|\#|#|", $input);
$count = count($input);

for($i = 0; $i < $count; $i++) {
$input[$i] = "obj.options[obj.options.length] = new Option('" . $input[$i] . "', '" . $i + 1 . "');\n";
echo $input[$i];
}

?>


Note: code typed OTF, not error checked etc

Readie

5:52 pm on Feb 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bleh, so tired last night I didn't even think this through properly.

$input = preg_replace('/[\r\n]{2}/m', '|##/|\#|#|', $input); 
$input = explode("|##/|\#|#|", $input);


That could be achieved with

$input = preg_split('/[\r\n]{2}/m', $input);