Forum Moderators: coopster

Message Too Old, No Replies

Array builder

         

Readie

6:00 am on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I have a fairly large array that I sometimes need to update (see [webmasterworld.com...] and [webmasterworld.com...] ) and it gets a little annoying when I have to re-order the damned thing when I need to insert a new variable in halfway through the list.

So, to solve my dilemna I created the following PhP script, which I thought I'd stick here in case anyone has similar issues :)

Big thanks to Rocknbil for showing me how to grab a backslash with regex :)

<?php

$reg = $_POST['reg'];
$arname = $_POST['arname'];
$cont = $_POST['content'];
$contl = preg_replace('/[\r\n]{2}/m', '|##/|\##|', $cont);

if(isset($reg) && $reg != "") {
$slash = '/';
$sel = ' checked';
}

if((isset($cont) && $cont != "") && (isset($arname) && $arname != "")) {
$contl = explode("|##/|\##|", $contl);
$rows = count($contl);
$out = '<textarea class="out" onfocus="select();">&#36;' . $arname . ' = array()&#59;';
for($i = 0; $i < $rows; $i++){
if(!preg_match("/'/", $contl[$i])) {
$out .= "\n" . '&#36;' . $arname . '[' . $i . '] = &#39;' . $slash . preg_replace('/[\/\\\\]"/', '&#34;', $contl[$i]) . $slash . '&#39;&#59;';
} elseif(!preg_match('/"/', $contl[$i])) {
$out .= "\n" . '&#36;' . $arname . '[' . $i . '] = &#34;' . $slash . preg_replace("/[\/\\\\]'/", '&#39;', $contl[$i]) . $slash . '&#34;&#59;';
} else {
$out .= "\n" . '&#36;' . $arname . '[' . $i . '] = ' . $slash . preg_replace('/[\/\\\\]"/', '&#34;', preg_replace("/[\/\\\\]'/", '&#39;', $contl[$i])) . $slash . ' ERROR: APOSTROPHEE AND QUOTATION';
}
}
$out .= '</textarea>';
}

$cont = '<html>
<head>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
<title>Array builder</title>
<style type="text/css">
input {border: #000 1px solid;padding: 1px;background-color: #ccc;font-size: 11px;color: #000;font-family: Verdana;width: 207px;}
input.check {border: 0px;padding: 0px;background-color: #fff;width: 13px;}
textarea {border: 1px solid;padding: 1px;background-color: #fff;font-size: 11px;color: #000;font-family: Lucida Console;height: 300px;width: 300px;overflow: auto;}
textarea.out {padding: 5px;background-color: #ccc;font-size: 11px;width: 400px;}
span {font-size: 11px;color: #000;font-family: Verdana;}
</style>
</head>
<body>
<span><b>Array builder</b><br><br>Created by Michael Read<br>Contact me by sending a PM to Readie at webmasterworld.com<br>This tool may be used freely for all purposes and without citation<hr><br>Enter each new array value on a new line.</span>
<form method="post">
<span><b>Array name: &#36;</b></span><input type="text" name="arname" value="' . $arname . '"><br>
<textarea name="content">' . preg_replace("/[\/\\\\]'/", "'", preg_replace('/[\/\\\\]"/', '"', $cont)) . '</textarea><br>
<span>Regex:</span><input class="check" type="checkbox" name="reg" value="yes"' . $sel . '><br>
<input type="submit" value="Create array">
</form>' . $out . '
</body>
</html>';

echo $cont;

?>

dreamcatcher

5:05 am on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for sharing Readie, I`m sure other people will find this useful. :)

dc

Readie

9:34 am on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh god hide it, it's terrible lol :)

Delete this...
$contl = preg_replace('/[\r\n]{2}/m', '|##/|\##|', $cont); 

Replace this...
$contl = explode("|##/|\##|", $contl); 
With...
$contl = preg_split('/[/r/n]{2}/m', $contl);

I'm not even going to begin addressing the rest I dislike about it :)

Matthew1980

7:04 pm on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie,

Are you saying that this is good or is bad? I'm confused now - which doesnt take much to do ;-p

Thanks for the post though, I am just starting out (as you know from previous posts) using/learning regexp, and I thought as this would be a good way to learn when I saw it.

Cheers,
MRb

dreamcatcher

10:32 pm on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, thats a good sign of learning if you are picking faults with your code after a month. ;)

dc

Readie

4:44 pm on May 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> Matt

It was useful for a while when I was using two ksorted arrays as comparison, but I've since switched over to an associative array, so I don't use it anymore.

>> Dream

That's a good way of looking at it :)