Page 2 of 3

Re: Split string and retain part before character match?

Posted: Fri Jun 13, 2008 6:02 pm
by JAB Creations
I start with...

Code: Select all

$$my_var2[3] == 'color: #333'
When I echo the final variable I get whatever the number is after substr(.

Instead of echoing '6' for substr(6 or '0' for substr(0 I want it to echo '333' from the first string in this post.

Re: Split string and retain part before character match?

Posted: Fri Jun 13, 2008 6:25 pm
by RobertGonzalez
Are you intending to use variable variables and comparisons?

Re: Split string and retain part before character match?

Posted: Fri Jun 13, 2008 6:39 pm
by JAB Creations
I just want to spit out the string which I'll use concatenation to spit out variables to be used by JavaScript (you may have noticed my posts in the clientside forum). Right now I'm just concentrating on getting the base functionality to work which if it wasn't for a #%^ing power outage for an hour would have been completed by now. IMHO I've gotten almost all of the work offloaded to the client. However just getting it to work and then getting it to work efficiently benefits me in the way that if I know how to do this when I can't offload to the client I'll be that much more prepared for coding it at that point in the future. :mrgreen:

...and as far as the big picture goes this is part of the session --> php var --> JavaScript var builder --> vars matched to existing select menu selectors --> visitor's theme information retained when they modify existing theme on my site versus having to redo the theme from scratch. So I'm roughly 90% finished (with this being the last semi-major part of the overall project).

If you'd like to see my standalone test case file later I'll be happy to post it in the critique forum. But first I want to finish working on it and implement this as well. 8)

Re: Split string and retain part before character match?

Posted: Fri Jun 13, 2008 7:35 pm
by RobertGonzalez
I am just trying to get a handle on why you are using variable variables the way you were. If all you want to do is grab the part of a string that includes the beginning of the string to a known character then you would do what Kieran sugested:

Code: Select all

<?php
$str = ".class { color: blue; }"
$chunk = substr($str, 0, strpos($str, '{'));
echo $chunk;
?>

Re: Split string and retain part before character match?

Posted: Fri Jun 13, 2008 10:42 pm
by Kieran Huggins
looks like you want something like:

Code: Select all

$str = ".class { color: blue; }";
$selector = substr($str, 0, strpos($str, '{'));
$rules = substr($str, strpos($str, '{') + 1, strpos($str, '}') - (strpos($str, '{')+1));

Re: Split string and retain part before character match?

Posted: Sun Jun 15, 2008 9:53 am
by JAB Creations
Thanks for your suggestions, I have this working pretty good now after a bit of effort though unfortunately it's a very conditional to implement your suggestions...

This works so long as all four properties (per selector) have values. If the first one does not then the second property's value is echoed as the first property's value.

Code: Select all

<?php
$pieces = explode('}', $_SESSION['css']);
$number = '0';
foreach($pieces as $key=>$value)
{
 $my_var1 = split(' {', $value);
 echo 'window[\'ii'.$key.'0\'] = \''.$my_var1[0]."';\n";
 $my_var2 = split(';', $my_var1[1]);
 
//rule1 = background-color
 $rules1 = substr($my_var1[1], strpos($my_var1[1], '{') + 19, strpos($my_var1[1], '}') - (strpos($my_var1[1], '{')-3));
 echo 'var ii'.$key.'1 = \''.$rules1."';\n";
 
//rule2 = background-image
 $rules2 = substr($my_var2[1], strpos($my_var2[1], '{') + 31, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-6));
 echo 'var ii'.$key.'2 = \''.$rules2."';\n";
 
//rule3 = border-color
 $rules3 = substr($my_var2[2], strpos($my_var2[2], '{') + 16, strpos($my_var2[2], '}') - (strpos($my_var2[2], '{')-6));
 echo 'var ii'.$key.'3 = \''.$rules3."';\n";
 
//rule4 = color
 $rules4 = substr($my_var2[3], strpos($my_var2[3], '{') + 9, strpos($my_var2[3], '}') - (strpos($my_var2[3], '{')-6));
 echo 'var ii'.$key.'4 = \''.$rules4."';\n";
}
?>
So if no background-color value is set then the background-image value will take it's place. You could imagine that would confuse the heck out of my visitors.

Using these same rules I'm going to attempt to determine what property each array key/value contains and then apply the desired rule.

I have a feeling this is going to get messy because as I'm looking at it I've been thinking about things like functions and such.

Any way this tid-bit below is a sort of a half-way point between the above code and what I'm going to attempt. Honestly I'm not sure when this becomes as inefficient as regex but if it's going to start creating the same amount of severload then it's not really going to help...

Code: Select all

$rules21 = substr($my_var2[1], strpos($my_var2[1], '{') + 0, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-16));
echo '<br /><br />'.$rules21.'<br /><br />';
if ($rules21 == 'background-image')
{
 $rules22 = substr($my_var2[1], strpos($my_var2[1], '{') + 31, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-6));
 echo 'var ii'.$key.'2 = \''.$rules22."';\n<br />";
}

Re: Split string and retain part before character match?

Posted: Sun Jun 15, 2008 10:26 am
by RobertGonzalez
Yeah, if you are going to be doing a lot of string manipulation on the same string(s) it may be better just to use a regex to wrap them all and do whatever it is you want to do to everything at once.

Re: Split string and retain part before character match?

Posted: Sun Jun 15, 2008 10:39 am
by JAB Creations
Here is my full standalone test file. I've made two $example variables available on lines 13 and 14, the first all four properties are set (so there is no issue). The second doesn't have the background-color (the first property expected by PHP) set so that is the known issue.

I'm thinking about going back to the regex though I'm sure I'll have to implement changes to that.

Also I know I could possibly move this stuff to JavaScript though like I said earlier that gives me less to figure out with PHP.

I'm curious what you guys think of the stand alone test file though I am not ready to post this in the critique forum...

Code: Select all

<?php session_name("member"); session_start();
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Standalone Test Page</title>
<html>
<head>
<script type="text/javascript">
//<![CDATA[
<?php
$example1 = '#welcome, #content div.border, #content div.bordernorc, #prompts div.scroll, #prompts #promptstabs div.current {background-color: #000; background-image: url(general/11/004.gif); border-color: #3f6; color: #f33;}#body, #prompts {background-color: #3cf; background-image: url(general/11/002.gif); border-color: #0c6; color: #96f;}';
$example2 = '#welcome, #content div.border, #content div.bordernorc, #prompts div.scroll, #prompts #promptstabs div.current {background-image: url(general/11/002.gif); color: #6f0;}body, html {background-color: #c9f; color: #cc0;}';
 
//$pieces = explode('}', $_SESSION['css']);
$pieces = explode('}', $example1);
//$pieces = explode('}', $example2);
 
$number = '0';
foreach($pieces as $key=>$value)
{
 $my_var1 = split(' {', $value);
 echo 'window[\'ii'.$key.'0\'] = \''.$my_var1[0]."';\n";
 $my_var2 = split(';', $my_var1[1]);
 
 $rules1 = substr($my_var1[1], strpos($my_var1[1], '{') + 19, strpos($my_var1[1], '}') - (strpos($my_var1[1], '{')-3));
 echo 'var ii'.$key.'1 = \''.$rules1."';\n";
 
 $rules2 = substr($my_var2[1], strpos($my_var2[1], '{') + 31, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-6));
 echo 'var ii'.$key.'2 = \''.$rules2."';\n";
 
 $rules3 = substr($my_var2[2], strpos($my_var2[2], '{') + 16, strpos($my_var2[2], '}') - (strpos($my_var2[2], '{')-6));
 echo 'var ii'.$key.'3 = \''.$rules3."';\n";
 
 $rules4 = substr($my_var2[3], strpos($my_var2[3], '{') + 9, strpos($my_var2[3], '}') - (strpos($my_var2[3], '{')-6));
 echo 'var ii'.$key.'4 = \''.$rules4."';\n";
}
?>
 
function fill_values(my_param)
{
 for (var i=0; i<3; i++) //26
 {
 var the_selector = i;
 var the_var_form = 'ce' + the_selector + '0';
 
 var the_var_form1 = 'ce' + the_selector + '1';
 var the_var_form2 = 'ce' + the_selector + '2';
 var the_var_form3 = 'ce' + the_selector + '3';
 var the_var_form4 = 'ce' + the_selector + '4';
 
 var s = 'ii' + my_param + '0';
 var s1 = 'ii' + my_param + '1';
 var s2 = 'ii' + my_param + '2';
 var s3 = 'ii' + my_param + '3';
 var s4 = 'ii' + my_param + '4';
 
 if (document.getElementById(the_var_form).value == this[s]) {alert(the_var_form + 'match at ii' + the_selector + '0');
 document.getElementById('ce' + i+ + '1').value = this[s1];
 document.getElementById('ce' + i+ + '2').value = this[s2];
 document.getElementById('ce' + i+ + '3').value = this[s3];
 document.getElementById('ce' + i+ + '4').value = this[s4];
 }
 }
}
 
 
function fill_values_exec()
{
 for (var i=0; i<3; i++) //26
 {
  fill_values(i);
 }
}
 
 
function color_editor_selectors()
{
 for (var i=0; i<document.getElementById('step_one').length; i++)
 {
  document.getElementById('ce' + i+ + '0').value = document.getElementById('step_one').options[i].value;
 }
}
 
function start() {
 color_editor_selectors();
 fill_values_exec();
} 
window.onload = start;
//]]>
</script>
<style type="text/css">
form {height: 300px; overflow: auto; width: 80%;}
</style>
</head>
 
<body>
 
<div>add select menu, then onload event, split after '}', foreach match, create 'ce' of input id that array item matches</div>
 
<form>
<fieldset>
<select id="step_one" tabindex="3">
 <option value="#welcome, #content div.border, #content div.bordernorc, #prompts div.scroll, #prompts #promptstabs div.current">Primary Set 1 (Content, Prompt Content)</option>
 <option value="#body, #prompts">Primary Set 2 (Body ID, Prompt Base)</option>
 <option value="body, html">Primary Set 3 (Body Element)</option>
 <option value="#content div.border div.border div.border, #head, #prompts #promptsbuttons, #prompts #promptstabs, #prompts #promptstabs div, #side div.inset, #side h2, #side form, div.footer, div.header, table tfoot, table thead, table tbody tr">Secondary Set</option>
 <option value="#bottom, #top">Frame (Bottom, Top)</option>
 <option value="h1">Header (Primary)</option>
 <option value="#prompts h2, h2, h3, h4, h5, h6">Header (Subheaders)</option>
 <option value="#content a:link, #side a:link, #promptsajax div.scroll a:link, #promptsajax div.noscroll a:link">Links</option>
 <option value="#content a:link:hover, #side a:link:hover, #promptsajax div.scroll a:link:hover, #promptsajax div.noscroll a:hover, #side div.inset div.scroll a:hover, #content a:link:focus, #side a:link:focus, #promptsajax div.scroll a:link:focus, #promptsajax div.noscroll a:focus, #side div.inset div.scroll a:focus">Links (Focus, Hover)</option>
 <option value="#content a:link:visited, #side a:link:visited, #promptsajax div.scroll a:link:visited, #promptsajax div.noscroll a:visited, #side div.inset div.scroll a:visited">Links (Visited)</option>
 <option value="#menua1, #menua2, #menua3, #menua4, #menua5, #menua6, #menua7, #menua8, #menua9, a.menuaa, a.tools, #toola1, #toola2, div.list div, #sidetoggle a, #sidetoggle div, #prompts #promptsbuttons div a, #prompts #promptstabs div a">Menus</option>
 <option value="#menua1 a:hover, #menua2 a:hover, #menua3 a:hover, #menua4 a:hover, #menua5 a:hover, #menua6 a:hover, #menua7 a:hover, #menua8 a:hover, #menua9 a:hover, #menua1 a:focus, #menua2 a:focus, #menua3 a:focus, #menua4 a:focus, #menua5 a:focus, #menua6 a:focus, #menua7 a:focus, #menua8 a:focus, #menua9 a:focus, a.menuaa:hover, a.tools:hover, #toola1:hover, #toola2:hover, div.list div:hover, #sidetoggle a:hover, #sidetoggle div:hover, a.menuaa:focus, a.tools:focus, #toola1:focus, #toola2:focus, div.list div:focus, #sidetoggle a:focus, #sidetoggle div:focus, #prompts #promptsbuttons a:focus, #prompts #promptsbuttons a:hover">Menus (Focus, Hover)</option>
 <option value="#menua1:visited, #menua2:visited, #menua3:visited, #menua4:visited, #menua5:visited, #menua6:visited, #menua7:visited, #menua8:visited, #menua9:visited, a.menuaa:visited, a.tools:visited, #toola1:visited, #toola2:visited, div.list div:visited, #sidetoggle a:visited, #sidetoggle div:visited, #prompts #promptsbuttons div a:visited, #prompts #promptstabs div a:visited">Menus (Visited)</option>
 <option value="#bottom div a">Music Player</option>
 <option value="#bottom div a:hover, #bottom div a:focus">Music Player (Hover, Focus)</option>
 <option value="form">Forms</option>
 <option value="form fieldset input.button, form fieldset input.text, form fieldset input.url, select, form fieldset textarea">Forms Fields (Buttons, Text)</option>
 <option value="form fieldset input.button:hover, form fieldset input.text:hover, form fieldset input.url:hover, select:hover, form fieldset textarea:hover, form fieldset input.button:focus, form fieldset input.text:focus, form fieldset input.url:focus, select:focus, form fieldset textarea">Forms Fields (Focus, Hover)</option>
 <option value="form fieldset label">Form Labels</option>
 <option value="ol, ul">Lists (Ordered, Unordered)</option>
 <option value="code">Code (Computer Code)</option>
 <option value="#head #location a:link, #head #location a:visited, .color1">Color 1 (Location)</option>
 <option value="#content span.subtitle, .color2">Color 2 (Subtitle)</option>
 <option value="p, p.noindent, ol li span, ul li span, .color3">Color 3 (Lists, Paragraph, Span)</option>
 <option value=".color4">Color 4 (Dashes)</option>
 <option value=".color5">Color 5 (Parenthesis, Quotes)</option>
</select>
<br style="clear: both;" />
<input id="ce00" name="ce00" type="text" value="" />
<input id="ce01" name="ce01" type="text" value="" />
<input id="ce02" name="ce02" type="text" value="" />
<input id="ce03" name="ce03" type="text" value="" />
<input id="ce04" name="ce04" type="text" value="" />
<br />
<input id="ce10" name="ce10" type="text" value="" />
<input id="ce11" name="ce11" type="text" value="" />
<input id="ce12" name="ce12" type="text" value="" />
<input id="ce13" name="ce13" type="text" value="" />
<input id="ce14" name="ce14" type="text" value="" />
<br />
<input id="ce20" name="ce20" type="text" value="" />
<input id="ce21" name="ce21" type="text" value="" />
<input id="ce22" name="ce22" type="text" value="" />
<input id="ce23" name="ce23" type="text" value="" />
<input id="ce24" name="ce24" type="text" value="" />
<br />
<input id="ce30" name="ce30" type="text" value="" />
<input id="ce31" name="ce31" type="text" value="" />
<input id="ce32" name="ce32" type="text" value="" />
<input id="ce33" name="ce33" type="text" value="" />
<input id="ce34" name="ce34" type="text" value="" />
<br />
<input id="ce40" name="ce40" type="text" value="" />
<input id="ce41" name="ce41" type="text" value="" />
<input id="ce42" name="ce42" type="text" value="" />
<input id="ce43" name="ce43" type="text" value="" />
<input id="ce44" name="ce44" type="text" value="" />
<br />
<input id="ce50" name="ce50" type="text" value="" />
<input id="ce51" name="ce51" type="text" value="" />
<input id="ce52" name="ce52" type="text" value="" />
<input id="ce53" name="ce53" type="text" value="" />
<input id="ce54" name="ce54" type="text" value="" />
<br />
<input id="ce60" name="ce60" type="text" value="" />
<input id="ce61" name="ce61" type="text" value="" />
<input id="ce62" name="ce62" type="text" value="" />
<input id="ce63" name="ce63" type="text" value="" />
<input id="ce64" name="ce64" type="text" value="" />
<br />
<input id="ce70" name="ce70" type="text" value="" />
<input id="ce71" name="ce71" type="text" value="" />
<input id="ce72" name="ce72" type="text" value="" />
<input id="ce73" name="ce73" type="text" value="" />
<input id="ce74" name="ce74" type="text" value="" />
<br />
<input id="ce80" name="ce80" type="text" value="" />
<input id="ce81" name="ce81" type="text" value="" />
<input id="ce82" name="ce82" type="text" value="" />
<input id="ce83" name="ce83" type="text" value="" />
<input id="ce84" name="ce84" type="text" value="" />
<br />
<input id="ce90" name="ce90" type="text" value="" />
<input id="ce91" name="ce91" type="text" value="" />
<input id="ce92" name="ce92" type="text" value="" />
<input id="ce93" name="ce93" type="text" value="" />
<input id="ce94" name="ce94" type="text" value="" />
<br />
<input id="ce100" name="ce100" type="text" value="" />
<input id="ce101" name="ce101" type="text" value="" />
<input id="ce102" name="ce102" type="text" value="" />
<input id="ce103" name="ce103" type="text" value="" />
<input id="ce104" name="ce104" type="text" value="" />
<br />
<input id="ce110" name="ce110" type="text" value="" />
<input id="ce111" name="ce111" type="text" value="" />
<input id="ce112" name="ce112" type="text" value="" />
<input id="ce113" name="ce113" type="text" value="" />
<input id="ce114" name="ce114" type="text" value="" />
<br />
<input id="ce120" name="ce120" type="text" value="" />
<input id="ce121" name="ce121" type="text" value="" />
<input id="ce122" name="ce122" type="text" value="" />
<input id="ce123" name="ce123" type="text" value="" />
<input id="ce124" name="ce124" type="text" value="" />
<br />
<input id="ce130" name="ce130" type="text" value="" />
<input id="ce131" name="ce131" type="text" value="" />
<input id="ce132" name="ce132" type="text" value="" />
<input id="ce133" name="ce133" type="text" value="" />
<input id="ce134" name="ce134" type="text" value="" />
<br />
<input id="ce140" name="ce140" type="text" value="" />
<input id="ce141" name="ce141" type="text" value="" />
<input id="ce142" name="ce142" type="text" value="" />
<input id="ce143" name="ce143" type="text" value="" />
<input id="ce144" name="ce144" type="text" value="" />
<br />
<input id="ce150" name="ce150" type="text" value="" />
<input id="ce151" name="ce151" type="text" value="" />
<input id="ce152" name="ce152" type="text" value="" />
<input id="ce153" name="ce153" type="text" value="" />
<input id="ce154" name="ce154" type="text" value="" />
<br />
<input id="ce160" name="ce160" type="text" value="" />
<input id="ce161" name="ce161" type="text" value="" />
<input id="ce162" name="ce162" type="text" value="" />
<input id="ce163" name="ce163" type="text" value="" />
<input id="ce164" name="ce164" type="text" value="" />
<br />
<input id="ce170" name="ce170" type="text" value="" />
<input id="ce171" name="ce171" type="text" value="" />
<input id="ce172" name="ce172" type="text" value="" />
<input id="ce173" name="ce173" type="text" value="" />
<input id="ce174" name="ce174" type="text" value="" />
<br />
<input id="ce180" name="ce180" type="text" value="" />
<input id="ce181" name="ce181" type="text" value="" />
<input id="ce182" name="ce182" type="text" value="" />
<input id="ce183" name="ce183" type="text" value="" />
<input id="ce184" name="ce184" type="text" value="" />
<br />
<input id="ce190" name="ce190" type="text" value="" />
<input id="ce191" name="ce191" type="text" value="" />
<input id="ce192" name="ce192" type="text" value="" />
<input id="ce193" name="ce193" type="text" value="" />
<input id="ce194" name="ce194" type="text" value="" />
<br />
<input id="ce200" name="ce200" type="text" value="" />
<input id="ce201" name="ce201" type="text" value="" />
<input id="ce202" name="ce202" type="text" value="" />
<input id="ce203" name="ce203" type="text" value="" />
<input id="ce204" name="ce204" type="text" value="" />
<br />
<input id="ce210" name="ce210" type="text" value="" />
<input id="ce211" name="ce211" type="text" value="" />
<input id="ce212" name="ce212" type="text" value="" />
<input id="ce213" name="ce213" type="text" value="" />
<input id="ce214" name="ce214" type="text" value="" />
<br />
<input id="ce220" name="ce220" type="text" value="" />
<input id="ce221" name="ce221" type="text" value="" />
<input id="ce222" name="ce222" type="text" value="" />
<input id="ce223" name="ce223" type="text" value="" />
<input id="ce224" name="ce224" type="text" value="" />
<br />
<input id="ce230" name="ce230" type="text" value="" />
<input id="ce231" name="ce231" type="text" value="" />
<input id="ce232" name="ce232" type="text" value="" />
<input id="ce233" name="ce233" type="text" value="" />
<input id="ce234" name="ce234" type="text" value="" />
<br />
<input id="ce240" name="ce240" type="text" value="" />
<input id="ce241" name="ce241" type="text" value="" />
<input id="ce242" name="ce242" type="text" value="" />
<input id="ce243" name="ce243" type="text" value="" />
<input id="ce244" name="ce244" type="text" value="" />
<br />
<input id="ce250" name="ce250" type="text" value="" />
<input id="ce251" name="ce251" type="text" value="" />
<input id="ce252" name="ce252" type="text" value="" />
<input id="ce253" name="ce253" type="text" value="" />
<input id="ce254" name="ce254" type="text" value="" />
</fieldset>
</form>
 
<div>
<?php
$pieces = explode('}', $_SESSION['css']);
$number = '0';
foreach($pieces as $key=>$value)
{
 $my_var1 = split(' {', $value);
 echo 'window[\'ii'.$key.'0\'] = \''.$my_var1[0]."';\n<br />";
 $my_var2 = split(';', $my_var1[1]);
 
 $rules1 = substr($my_var1[1], strpos($my_var1[1], '{') + 19, strpos($my_var1[1], '}') - (strpos($my_var1[1], '{')-3));
 echo 'var ii'.$key.'1 = \''.$rules1."';\n<br />";
 
 $rules2 = substr($my_var2[1], strpos($my_var2[1], '{') + 31, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-6));
 echo 'var ii'.$key.'2 = \''.$rules2."';\n<br />";
 
 $rules3 = substr($my_var2[2], strpos($my_var2[2], '{') + 16, strpos($my_var2[2], '}') - (strpos($my_var2[2], '{')-6));
 echo 'var ii'.$key.'3 = \''.$rules3."';\n<br />";
 
 $rules4 = substr($my_var2[3], strpos($my_var2[3], '{') + 9, strpos($my_var2[3], '}') - (strpos($my_var2[3], '{')-6));
 echo 'var ii'.$key.'4 = \''.$rules4."';\n<br /><br />";
}
 
// Was going to wrap each rule but it's not working out as I was originally thinking...
//$rules11 = substr($my_var1[1], strpos($my_var1[1], '{') + 0, strpos($my_var1[1], '}') - (strpos($my_var1[1], '{')-16));
//if ($rules11 == 'background-color')
//{}
 
?>
</div>
 
</body>
</html>

Re: Split string and retain part before character match?

Posted: Sun Jun 15, 2008 3:34 pm
by JAB Creations
Here is an oddity...the $my_var2[0]...if you change the '0' on line 294 to '1' it will strip the string containing the property to 4 characters for the $my_var2[0] but only three for the following functions...they are echoed on lines 307-310.

Any ideas why? :mrgreen:

Code: Select all

<?php session_name("member"); session_start();
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>SPLIT - Standalone Test Page</title>
<html>
<head>
<script type="text/javascript">
//<![CDATA[
<?php
$pieces = explode('}', $_SESSION['css']);
$number = '0';
foreach($pieces as $key=>$value)
{
 $my_var1 = split(' {', $value);
 echo "window['ii".$key."0'] = '".$my_var1[0]."';\n";
 $my_var2 = split(';', $my_var1[1]);
 
 $rules1 = substr($my_var1[1], strpos($my_var1[1], '{') + 19, strpos($my_var1[1], '}') - (strpos($my_var1[1], '{')-3));
 echo "var ii".$key."1 = '".$rules1."';\n";
 
 $rules2 = substr($my_var2[1], strpos($my_var2[1], '{') + 31, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-6));
 echo "var ii".$key."2 = '".$rules2."';\n";
 
 $rules3 = substr($my_var2[2], strpos($my_var2[2], '{') + 16, strpos($my_var2[2], '}') - (strpos($my_var2[2], '{')-6));
 echo "var ii".$key."3 = '".$rules3."';\n";
 
 $rules4 = substr($my_var2[3], strpos($my_var2[3], '{') + 9, strpos($my_var2[3], '}') - (strpos($my_var2[3], '{')-6));
 echo "var ii".$key."4 = '".$rules4."';\n";
}
?>
 
function fill_values(my_param)
{
 for (var i=0; i<3; i++) //26
 {
 var the_selector = i;
 var the_var_form = 'ce' + the_selector + '0';
 
 var the_var_form1 = 'ce' + the_selector + '1';
 var the_var_form2 = 'ce' + the_selector + '2';
 var the_var_form3 = 'ce' + the_selector + '3';
 var the_var_form4 = 'ce' + the_selector + '4';
 
 var s = 'ii' + my_param + '0';
 var s1 = 'ii' + my_param + '1';
 var s2 = 'ii' + my_param + '2';
 var s3 = 'ii' + my_param + '3';
 var s4 = 'ii' + my_param + '4';
 
 if (document.getElementById(the_var_form).value == this[s]) {alert(the_var_form + 'match at ii' + the_selector + '0');
 document.getElementById('ce' + i+ + '1').value = this[s1];
 document.getElementById('ce' + i+ + '2').value = this[s2];
 document.getElementById('ce' + i+ + '3').value = this[s3];
 document.getElementById('ce' + i+ + '4').value = this[s4];
 }
 }
}
 
 
function fill_values_exec()
{
 for (var i=0; i<3; i++) //26
 {
  fill_values(i);
 }
}
 
 
function color_editor_selectors()
{
 for (var i=0; i<document.getElementById('step_one').length; i++)
 {
  document.getElementById('ce' + i+ + '0').value = document.getElementById('step_one').options[i].value;
 }
}
 
function start() {
 color_editor_selectors();
 fill_values_exec();
}
window.onload = start;
//]]>
</script>
<style type="text/css">
form {height: 300px; overflow: auto; width: 80%;}
</style>
</head>
 
<body>
 
<h1>split</h1>
 
<form>
<fieldset>
<select id="step_one" tabindex="3">
 <option value="#welcome, #content div.border, #content div.bordernorc, #prompts div.scroll, #prompts #promptstabs div.current">Primary Set 1 (Content, Prompt Content)</option>
 <option value="#body, #prompts">Primary Set 2 (Body ID, Prompt Base)</option>
 <option value="body, html">Primary Set 3 (Body Element)</option>
 <option value="#content div.border div.border div.border, #head, #prompts #promptsbuttons, #prompts #promptstabs, #prompts #promptstabs div, #side div.inset, #side h2, #side form, div.footer, div.header, table tfoot, table thead, table tbody tr">Secondary Set</option>
 <option value="#bottom, #top">Frame (Bottom, Top)</option>
 <option value="h1">Header (Primary)</option>
 <option value="#prompts h2, h2, h3, h4, h5, h6">Header (Subheaders)</option>
 <option value="#content a:link, #side a:link, #promptsajax div.scroll a:link, #promptsajax div.noscroll a:link">Links</option>
 <option value="#content a:link:hover, #side a:link:hover, #promptsajax div.scroll a:link:hover, #promptsajax div.noscroll a:hover, #side div.inset div.scroll a:hover, #content a:link:focus, #side a:link:focus, #promptsajax div.scroll a:link:focus, #promptsajax div.noscroll a:focus, #side div.inset div.scroll a:focus">Links (Focus, Hover)</option>
 <option value="#content a:link:visited, #side a:link:visited, #promptsajax div.scroll a:link:visited, #promptsajax div.noscroll a:visited, #side div.inset div.scroll a:visited">Links (Visited)</option>
 <option value="#menua1, #menua2, #menua3, #menua4, #menua5, #menua6, #menua7, #menua8, #menua9, a.menuaa, a.tools, #toola1, #toola2, div.list div, #sidetoggle a, #sidetoggle div, #prompts #promptsbuttons div a, #prompts #promptstabs div a">Menus</option>
 <option value="#menua1 a:hover, #menua2 a:hover, #menua3 a:hover, #menua4 a:hover, #menua5 a:hover, #menua6 a:hover, #menua7 a:hover, #menua8 a:hover, #menua9 a:hover, #menua1 a:focus, #menua2 a:focus, #menua3 a:focus, #menua4 a:focus, #menua5 a:focus, #menua6 a:focus, #menua7 a:focus, #menua8 a:focus, #menua9 a:focus, a.menuaa:hover, a.tools:hover, #toola1:hover, #toola2:hover, div.list div:hover, #sidetoggle a:hover, #sidetoggle div:hover, a.menuaa:focus, a.tools:focus, #toola1:focus, #toola2:focus, div.list div:focus, #sidetoggle a:focus, #sidetoggle div:focus, #prompts #promptsbuttons a:focus, #prompts #promptsbuttons a:hover">Menus (Focus, Hover)</option>
 <option value="#menua1:visited, #menua2:visited, #menua3:visited, #menua4:visited, #menua5:visited, #menua6:visited, #menua7:visited, #menua8:visited, #menua9:visited, a.menuaa:visited, a.tools:visited, #toola1:visited, #toola2:visited, div.list div:visited, #sidetoggle a:visited, #sidetoggle div:visited, #prompts #promptsbuttons div a:visited, #prompts #promptstabs div a:visited">Menus (Visited)</option>
 <option value="#bottom div a">Music Player</option>
 <option value="#bottom div a:hover, #bottom div a:focus">Music Player (Hover, Focus)</option>
 <option value="form">Forms</option>
 <option value="form fieldset input.button, form fieldset input.text, form fieldset input.url, select, form fieldset textarea">Forms Fields (Buttons, Text)</option>
 <option value="form fieldset input.button:hover, form fieldset input.text:hover, form fieldset input.url:hover, select:hover, form fieldset textarea:hover, form fieldset input.button:focus, form fieldset input.text:focus, form fieldset input.url:focus, select:focus, form fieldset textarea">Forms Fields (Focus, Hover)</option>
 <option value="form fieldset label">Form Labels</option>
 <option value="ol, ul">Lists (Ordered, Unordered)</option>
 <option value="code">Code (Computer Code)</option>
 <option value="#head #location a:link, #head #location a:visited, .color1">Color 1 (Location)</option>
 <option value="#content span.subtitle, .color2">Color 2 (Subtitle)</option>
 <option value="p, p.noindent, ol li span, ul li span, .color3">Color 3 (Lists, Paragraph, Span)</option>
 <option value=".color4">Color 4 (Dashes)</option>
 <option value=".color5">Color 5 (Parenthesis, Quotes)</option>
</select>
<br style="clear: both;" />
<input id="ce00" name="ce00" type="text" value="" />
<input id="ce01" name="ce01" type="text" value="" />
<input id="ce02" name="ce02" type="text" value="" />
<input id="ce03" name="ce03" type="text" value="" />
<input id="ce04" name="ce04" type="text" value="" />
<br />
<input id="ce10" name="ce10" type="text" value="" />
<input id="ce11" name="ce11" type="text" value="" />
<input id="ce12" name="ce12" type="text" value="" />
<input id="ce13" name="ce13" type="text" value="" />
<input id="ce14" name="ce14" type="text" value="" />
<br />
<input id="ce20" name="ce20" type="text" value="" />
<input id="ce21" name="ce21" type="text" value="" />
<input id="ce22" name="ce22" type="text" value="" />
<input id="ce23" name="ce23" type="text" value="" />
<input id="ce24" name="ce24" type="text" value="" />
<br />
<input id="ce30" name="ce30" type="text" value="" />
<input id="ce31" name="ce31" type="text" value="" />
<input id="ce32" name="ce32" type="text" value="" />
<input id="ce33" name="ce33" type="text" value="" />
<input id="ce34" name="ce34" type="text" value="" />
<br />
<input id="ce40" name="ce40" type="text" value="" />
<input id="ce41" name="ce41" type="text" value="" />
<input id="ce42" name="ce42" type="text" value="" />
<input id="ce43" name="ce43" type="text" value="" />
<input id="ce44" name="ce44" type="text" value="" />
<br />
<input id="ce50" name="ce50" type="text" value="" />
<input id="ce51" name="ce51" type="text" value="" />
<input id="ce52" name="ce52" type="text" value="" />
<input id="ce53" name="ce53" type="text" value="" />
<input id="ce54" name="ce54" type="text" value="" />
<br />
<input id="ce60" name="ce60" type="text" value="" />
<input id="ce61" name="ce61" type="text" value="" />
<input id="ce62" name="ce62" type="text" value="" />
<input id="ce63" name="ce63" type="text" value="" />
<input id="ce64" name="ce64" type="text" value="" />
<br />
<input id="ce70" name="ce70" type="text" value="" />
<input id="ce71" name="ce71" type="text" value="" />
<input id="ce72" name="ce72" type="text" value="" />
<input id="ce73" name="ce73" type="text" value="" />
<input id="ce74" name="ce74" type="text" value="" />
<br />
<input id="ce80" name="ce80" type="text" value="" />
<input id="ce81" name="ce81" type="text" value="" />
<input id="ce82" name="ce82" type="text" value="" />
<input id="ce83" name="ce83" type="text" value="" />
<input id="ce84" name="ce84" type="text" value="" />
<br />
<input id="ce90" name="ce90" type="text" value="" />
<input id="ce91" name="ce91" type="text" value="" />
<input id="ce92" name="ce92" type="text" value="" />
<input id="ce93" name="ce93" type="text" value="" />
<input id="ce94" name="ce94" type="text" value="" />
<br />
<input id="ce100" name="ce100" type="text" value="" />
<input id="ce101" name="ce101" type="text" value="" />
<input id="ce102" name="ce102" type="text" value="" />
<input id="ce103" name="ce103" type="text" value="" />
<input id="ce104" name="ce104" type="text" value="" />
<br />
<input id="ce110" name="ce110" type="text" value="" />
<input id="ce111" name="ce111" type="text" value="" />
<input id="ce112" name="ce112" type="text" value="" />
<input id="ce113" name="ce113" type="text" value="" />
<input id="ce114" name="ce114" type="text" value="" />
<br />
<input id="ce120" name="ce120" type="text" value="" />
<input id="ce121" name="ce121" type="text" value="" />
<input id="ce122" name="ce122" type="text" value="" />
<input id="ce123" name="ce123" type="text" value="" />
<input id="ce124" name="ce124" type="text" value="" />
<br />
<input id="ce130" name="ce130" type="text" value="" />
<input id="ce131" name="ce131" type="text" value="" />
<input id="ce132" name="ce132" type="text" value="" />
<input id="ce133" name="ce133" type="text" value="" />
<input id="ce134" name="ce134" type="text" value="" />
<br />
<input id="ce140" name="ce140" type="text" value="" />
<input id="ce141" name="ce141" type="text" value="" />
<input id="ce142" name="ce142" type="text" value="" />
<input id="ce143" name="ce143" type="text" value="" />
<input id="ce144" name="ce144" type="text" value="" />
<br />
<input id="ce150" name="ce150" type="text" value="" />
<input id="ce151" name="ce151" type="text" value="" />
<input id="ce152" name="ce152" type="text" value="" />
<input id="ce153" name="ce153" type="text" value="" />
<input id="ce154" name="ce154" type="text" value="" />
<br />
<input id="ce160" name="ce160" type="text" value="" />
<input id="ce161" name="ce161" type="text" value="" />
<input id="ce162" name="ce162" type="text" value="" />
<input id="ce163" name="ce163" type="text" value="" />
<input id="ce164" name="ce164" type="text" value="" />
<br />
<input id="ce170" name="ce170" type="text" value="" />
<input id="ce171" name="ce171" type="text" value="" />
<input id="ce172" name="ce172" type="text" value="" />
<input id="ce173" name="ce173" type="text" value="" />
<input id="ce174" name="ce174" type="text" value="" />
<br />
<input id="ce180" name="ce180" type="text" value="" />
<input id="ce181" name="ce181" type="text" value="" />
<input id="ce182" name="ce182" type="text" value="" />
<input id="ce183" name="ce183" type="text" value="" />
<input id="ce184" name="ce184" type="text" value="" />
<br />
<input id="ce190" name="ce190" type="text" value="" />
<input id="ce191" name="ce191" type="text" value="" />
<input id="ce192" name="ce192" type="text" value="" />
<input id="ce193" name="ce193" type="text" value="" />
<input id="ce194" name="ce194" type="text" value="" />
<br />
<input id="ce200" name="ce200" type="text" value="" />
<input id="ce201" name="ce201" type="text" value="" />
<input id="ce202" name="ce202" type="text" value="" />
<input id="ce203" name="ce203" type="text" value="" />
<input id="ce204" name="ce204" type="text" value="" />
<br />
<input id="ce210" name="ce210" type="text" value="" />
<input id="ce211" name="ce211" type="text" value="" />
<input id="ce212" name="ce212" type="text" value="" />
<input id="ce213" name="ce213" type="text" value="" />
<input id="ce214" name="ce214" type="text" value="" />
<br />
<input id="ce220" name="ce220" type="text" value="" />
<input id="ce221" name="ce221" type="text" value="" />
<input id="ce222" name="ce222" type="text" value="" />
<input id="ce223" name="ce223" type="text" value="" />
<input id="ce224" name="ce224" type="text" value="" />
<br />
<input id="ce230" name="ce230" type="text" value="" />
<input id="ce231" name="ce231" type="text" value="" />
<input id="ce232" name="ce232" type="text" value="" />
<input id="ce233" name="ce233" type="text" value="" />
<input id="ce234" name="ce234" type="text" value="" />
<br />
<input id="ce240" name="ce240" type="text" value="" />
<input id="ce241" name="ce241" type="text" value="" />
<input id="ce242" name="ce242" type="text" value="" />
<input id="ce243" name="ce243" type="text" value="" />
<input id="ce244" name="ce244" type="text" value="" />
<br />
<input id="ce250" name="ce250" type="text" value="" />
<input id="ce251" name="ce251" type="text" value="" />
<input id="ce252" name="ce252" type="text" value="" />
<input id="ce253" name="ce253" type="text" value="" />
<input id="ce254" name="ce254" type="text" value="" />
</fieldset>
</form>
 
<div>
<?php
$example1 = '#welcome, #content div.border, #content div.bordernorc, #prompts div.scroll, #prompts #promptstabs div.current {background-color: #000; background-image: url(general/11/004.gif); border-color: #3f6; color: #f33;}#body, #prompts {background-color: #3cf; background-image: url(general/11/002.gif); border-color: #0c6; color: #96f;}';
$pieces = explode('}', $example1);
//$pieces = explode('}', $_SESSION['css']);
$number = '0';
 
function associate($given_value)
{
$rule_a = substr($given_value, strpos($given_value, '{') + 0, strpos($given_value, '}') - (strpos($given_value, '{')-4));
return $rule_a;
}
 
foreach($pieces as $key=>$value)
{
 $my_var1 = split(' {', $value);
 echo "window['ii".$key."0'] = '".$my_var1[0]."';\n<br />";
 $my_var2 = split(';', $my_var1[1]);
 
echo $my_var2[0].'<br />';
echo $my_var2[0].'<br />'.'<br />'.'<br />'.'<br />';
 
echo associate($my_var2[0]).'<br />';
echo associate($my_var2[1]).'<br />';
echo associate($my_var2[2]).'<br />';
echo associate($my_var2[3]).'<br /><br />';
 
//rule1 = background-color
 $rules1 = substr($my_var1[1], strpos($my_var1[1], '{') + 19, strpos($my_var1[1], '}') - (strpos($my_var1[1], '{')-3));
 //echo 'var ii'.$key.'1 = ''.$rules1."';\n<br />";
 
//rule2 = background-image
 $rules2 = substr($my_var2[1], strpos($my_var2[1], '{') + 31, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-6));
 //echo 'var ii'.$key.'2 = ''.$rules2."';\n<br />";
 
//rule3 = border-color
 $rules3 = substr($my_var2[2], strpos($my_var2[2], '{') + 16, strpos($my_var2[2], '}') - (strpos($my_var2[2], '{')-6));
 //echo 'var ii'.$key.'3 = ''.$rules3."';\n<br />";
 
//rule4 = color
 $rules4 = substr($my_var2[3], strpos($my_var2[3], '{') + 9, strpos($my_var2[3], '}') - (strpos($my_var2[3], '{')-6));
 //echo 'var ii'.$key.'4 = ''.$rules4."';\n<br /><br />";
}
 
// Was going to wrap each rule but it's not working out as I was originally thinking...
//$rules11 = substr($my_var1[1], strpos($my_var1[1], '{') + 0, strpos($my_var1[1], '}') - (strpos($my_var1[1], '{')-16));
//if ($rules11 == 'background-color')
//{}
 
?>
</div>
 
</body>
</html>
Edit #1 - Did not compile.
Edit #2 - Added comments about which CSS properties are being selected.

Re: Split string and retain part before character match?

Posted: Sun Jun 15, 2008 3:59 pm
by Kieran Huggins
i must concur with my most learned colleague Everah: this has achieved regex mass.

Want to take a crack at a pattern?

Re: Split string and retain part before character match?

Posted: Sun Jun 15, 2008 4:32 pm
by Ambush Commander
The test file doesn't compile, and I can't figure out what it means by echo 'window['ii'.$key.'0']

But, even going off a quick glance, it looks like there's a lot of repetition and a lot of code that could be cleaned up.

Also, since we're dealing with CSS, it may make more sense to use a legitimate parser. CSSTidy has one you can rip out and use very quickly. Otherwise you'll end up drowning in a sea of edge cases... and regexes can't help you there.

Re: Split string and retain part before character match?

Posted: Sun Jun 15, 2008 5:37 pm
by JAB Creations
I've corrected the problems with the last test file in the post if you want to copy it again and test it out.

The JavaScript works fine; I'm not sure how to describe it other then variable variables.

There is no CSS here, just stuff that will be passed and later used as CSS. :wink:

Great things are usually done out of necessity and I'm starting to ponder if this is worth going to the extent I've been pursuing it through PHP.

I'm starting to think I should probably just create a variable with an associated variable that I can split (in JavaScript) in to an array and pick at it from there.

So I could just have PHP create variables that look like this in JavaScript...

$ii01 = 'body';
$ii02 = 'background-color: #f00; color: #444;';
$ii11 = 'div';
$ii12 = 'background-color: #f00; color: #444;';
$ii21 = 'span';
$ii22 = 'background-color: #f00; color: #444;';


...and use split at that point. I was thinking something along those lines originally though I wanted to see what I could do with PHP.

However since this is ultimately exporting from the server to the client I have little reason to do much of anything with the original string stored in the session variable.

Yes the code that if I went to the extremes I was attempting with PHP would have produced (if a visitor used all possible values) over a hundred variables in JavaScript by PHP.

Since the selectors vary in length I can't use the substr/strpos combo here however I think that technique is pretty frigin sweet so I'll save it in my archives. :mrgreen:

My internal clock's sun is setting in the west so once I regain consciousness tomorrow I'll go ahead and post my updated PHP code (full test case and I'll check this time before I post to ensure that it does work).

I am confident after that I can get the JavaScript code to work fine. I'm not as concerned about performance in JavaScript as I am with PHP though I'll still be open to suggestions. I'm also open to suggestions from what I currently have right now.

Thanks for all your input and support! :)

Re: Split string and retain part before character match?

Posted: Sun Jun 15, 2008 6:13 pm
by Ambush Commander
I still haven't the foggiest what you're trying to do, and the test file doesn't make any sense at all. Alright, so I've got all those inputs, and I've got some text underneath; am I supposed to submit something, or change something or what?
describe it other then variable variables.
The usual way is to use associative arrays. I.e.

Code: Select all

$container[$my_var2[3]] == 'color: #333';
This makes things far more understandable, and you can save everything by using $container.

Unfortunately, whatever you've been trying to do has been extremely obfuscated by your unfortunate choice of variable and function names. Let's go bit by bit.

Code: Select all

$pieces = explode('}', $_SESSION['css']);
$number = '0';
foreach($pieces as $key=>$value)
{
 $my_var1 = split(' {', $value);
 echo "window['ii".$key."0'] = '".$my_var1[0]."';\n";
 $my_var2 = split(';', $my_var1[1]);
 
 $rules1 = substr($my_var1[1], strpos($my_var1[1], '{') + 19, strpos($my_var1[1], '}') - (strpos($my_var1[1], '{')-3));
 echo "var ii".$key."1 = '".$rules1."';\n";
 
 $rules2 = substr($my_var2[1], strpos($my_var2[1], '{') + 31, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-6));
 echo "var ii".$key."2 = '".$rules2."';\n";
 
 $rules3 = substr($my_var2[2], strpos($my_var2[2], '{') + 16, strpos($my_var2[2], '}') - (strpos($my_var2[2], '{')-6));
 echo "var ii".$key."3 = '".$rules3."';\n";
 
 $rules4 = substr($my_var2[3], strpos($my_var2[3], '{') + 9, strpos($my_var2[3], '}') - (strpos($my_var2[3], '{')-6));
 echo "var ii".$key."4 = '".$rules4."';\n";
}
What you have here is a miniaturized CSS parser. A very rudimentary one. Now, if you had named things properly, they might be a little easier to understand:

Code: Select all

$declarations = explode('}', $_SESSION['css']);
foreach($declarations as $i => $decl)
{
 list($selector, $prop_string) = explode(' {', $decl);
 echo "window['ii".$i."0'] = '".$selector."';\n";
 $props = explode(';', $prop_string);
 foreach ($props as $prop) {
    // ??
 }
}
P.S. split() uses regexps. You don't need them. Use explode().

Now, I've left the // ?? in for a very specific reason: I don't know what you're doing with all these substrings on my_var1 and my_var2. You've got magic numbers with no explanation what they do (19, 31, 16, 9).

I tried it on some of the examples you posted earlier (I don't know what the contents of $_SESSION should be, since you never write to it in the script). The output is absolute nonsense.

Now, I'm kind of a dumb guy. So I need comments and informative variable names in order to figure out what's going on. So, appealing to my dumbness, I don't understand what's going on. Please explain.

As for some of your questions:
So I could just have PHP create variables that look like this in JavaScript...
That looks more like PHP to me.
...and use split at that point
With that data, I would have exploded $iix2 (where x is an integer) on semicolon, then on each element of the resulting array exploded on semicolon and setup a $key => $val
However since this is ultimately exporting from the server to the client I have little reason to do much of anything with the original string stored in the session variable.
What data do you have on the server? What data do you have on the client?
Since the selectors vary in length I can't use the substr/strpos combo here however I think that technique is pretty frigin sweet so I'll save it in my archives.
Use substr/strpos when you're operating on the selector. The whole point of using strpos is that it will determine the location necessary when you need it.

Better yet, use strcspn().

Re: Split string and retain part before character match?

Posted: Mon Jun 16, 2008 1:43 pm
by JAB Creations
This standalone script is the last major component in what is essentially a cycle of how the data (primarily the CSS selectors) are handled. In essence the selectors are only defined in the select menu. They are carried all the way through $_POST, $SESSION, $_COOKIE, the validator CSS temp file (validated by the CSS validator for the import feature), and of course compared here in this the last stage. Essentially my editor will support versioning where if I change a selector slightly only that particular selector may be lost if the visitor proceeds with editing their theme after a recent update.

All the hidden inputs are hidden on the editor, they're just visible to make it a bit easier for me to know what exactly is going on.

I originally had commented what each of the rules were though I added them during the original post. I've added them back. I've also modified them (though commented them out from executing) on the transitional version of the page below. The page below still uses PHP to generate the individual JavaScript variables. I'll be working this afternoon on a new function that splits the 'sets' of CSS properties/values. I'm surprised I didn't realize I could use explode (as I've been using it a lot of late) instead of split.
Ambush Commander wrote:What data do you have on the server? What data do you have on the client?
The session variable is being sliced up here in to JavaScript variables that will at the client be used to refill the input fields which means the data from the previous $_POST will be part of the current editor 'session' in a sense. Without the setup of this test file every time you use the editor you start from scratch. Once this works as desired the input fields will be given the data so that you can continue across countless $_POST requests.

Depending on how my adventures with JavaScript proceeds this may or may not be all I need to do with PHP. Of course the more I have the client handle the better.

Below I've made the <body> PHP generate less refined but less server intensive code to generate the JavaScript variables (in the body so they are easy to view). I've retained the older PHP in the <head> so you can still see the desired effects. Notice that before the file is executed (when you directly look at the code below) none of the form fields have any default values (well other then blank).

Code: Select all

<?php session_name("member"); session_start();
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>REGEX - Standalone Test Page</title>
<html>
<head>
<script type="text/javascript">
//<![CDATA[
<?php
$pieces = explode('}', $_SESSION['css']);
$number = '0';
foreach($pieces as $key=>$value)
{
$my_var1 = split(' {', $value);
$value2 = $key.'0\'] = \''.$my_var1[0]."';\n";
echo 'window[\'ii'.$value2;
 
$my_var2 = split(';', $my_var1[1]);
 
// js var output: background-color
$value31 = split('#', $my_var2[0]);
echo 'var ii'.$key.'1 = \''.$value31[1]."';\n";
 
// js var output: background-image
$value321 = split('general/', $my_var2[1]);
$value322 = split('\.', $value321[1]);
echo 'var ii'.$key.'2 = \''.$value322[0]."';\n";
 
// js var output: border-color
$value33 = split('#', $my_var2[2]);
echo 'var ii'.$key.'3 = \''.$value33[1]."';\n";
 
// js var output: color
$value34 = split('#', $my_var2[3]);
echo 'var ii'.$key.'4 = \''.$value34[1]."';\n";
}
?>
 
function var_var()
{
 var i=1;
 window['ii' + i] = 'Marco';
 alert('got ' + ii1);
 alert(ii00);
}
 
function match_strings()
{
 var i = '0';
 var the_selector = i;
 var the_var_form = 'ce' + the_selector + '0';
 var the_var_import = 'ii00';
 if (document.getElementById('ce00').value == ii00) {alert(the_var_form + ' match at ii' + the_selector + '0');}
 else {alert('no match');}
}
 
function fill_values(my_param)
{
 for (var i=0; i<3; i++) //26
 {
  var the_selector = i;
  var the_var_form = 'ce' + the_selector + '0';
  var the_var_form1 = 'ce' + the_selector + '1';
  var the_var_form2 = 'ce' + the_selector + '2';
  var the_var_form3 = 'ce' + the_selector + '3';
  var the_var_form4 = 'ce' + the_selector + '4';
  var s = 'ii' + my_param + '0';
  var s1 = 'ii' + my_param + '1';
  var s2 = 'ii' + my_param + '2';
  var s3 = 'ii' + my_param + '3';
  var s4 = 'ii' + my_param + '4';
  if (document.getElementById(the_var_form).value == this[s])
  {
   alert(the_var_form + 'match at ii' + the_selector + '0');
   document.getElementById('ce' + i+ + '1').value = this[s1];
   document.getElementById('ce' + i+ + '2').value = this[s2];
   document.getElementById('ce' + i+ + '3').value = this[s3];
   document.getElementById('ce' + i+ + '4').value = this[s4];
  }
 }
}
 
function fill_values_exec()
{
 for (var i=0; i<3; i++) //26
 {
  fill_values(i);
 }
}
 
function color_editor_selectors()
{
 for (var i=0; i<document.getElementById('step_one').length; i++)
 {
  document.getElementById('ce' + i+ + '0').value = document.getElementById('step_one').options[i].value;
  //alert('ce' + i+ + '0');
 }
}
 
function start() {
 color_editor_selectors();
 fill_values_exec();
} 
window.onload = start;
//]]>
</script>
<style type="text/css">
b.bad {color: #f00;}
b.good {color: #0f0;}
form {height: 300px; overflow: auto; width: 80%;}
</style>
</head>
 
<body>
 
<form>
<fieldset>
<select id="step_one" tabindex="3">
 <option value="#welcome, #content div.border, #content div.bordernorc, #prompts div.scroll, #prompts #promptstabs div.current">Primary Set 1 (Content, Prompt Content)</option>
 <option value="#body, #prompts">Primary Set 2 (Body ID, Prompt Base)</option>
 <option value="body, html">Primary Set 3 (Body Element)</option>
 <option value="#content div.border div.border div.border, #head, #prompts #promptsbuttons, #prompts #promptstabs, #prompts #promptstabs div, #side div.inset, #side h2, #side form, div.footer, div.header, table tfoot, table thead, table tbody tr">Secondary Set</option>
 <option value="#bottom, #top">Frame (Bottom, Top)</option>
 <option value="h1">Header (Primary)</option>
 <option value="#prompts h2, h2, h3, h4, h5, h6">Header (Subheaders)</option>
 <option value="#content a:link, #side a:link, #promptsajax div.scroll a:link, #promptsajax div.noscroll a:link">Links</option>
 <option value="#content a:link:hover, #side a:link:hover, #promptsajax div.scroll a:link:hover, #promptsajax div.noscroll a:hover, #side div.inset div.scroll a:hover, #content a:link:focus, #side a:link:focus, #promptsajax div.scroll a:link:focus, #promptsajax div.noscroll a:focus, #side div.inset div.scroll a:focus">Links (Focus, Hover)</option>
 <option value="#content a:link:visited, #side a:link:visited, #promptsajax div.scroll a:link:visited, #promptsajax div.noscroll a:visited, #side div.inset div.scroll a:visited">Links (Visited)</option>
 <option value="#menua1, #menua2, #menua3, #menua4, #menua5, #menua6, #menua7, #menua8, #menua9, a.menuaa, a.tools, #toola1, #toola2, div.list div, #sidetoggle a, #sidetoggle div, #prompts #promptsbuttons div a, #prompts #promptstabs div a">Menus</option>
 <option value="#menua1 a:hover, #menua2 a:hover, #menua3 a:hover, #menua4 a:hover, #menua5 a:hover, #menua6 a:hover, #menua7 a:hover, #menua8 a:hover, #menua9 a:hover, #menua1 a:focus, #menua2 a:focus, #menua3 a:focus, #menua4 a:focus, #menua5 a:focus, #menua6 a:focus, #menua7 a:focus, #menua8 a:focus, #menua9 a:focus, a.menuaa:hover, a.tools:hover, #toola1:hover, #toola2:hover, div.list div:hover, #sidetoggle a:hover, #sidetoggle div:hover, a.menuaa:focus, a.tools:focus, #toola1:focus, #toola2:focus, div.list div:focus, #sidetoggle a:focus, #sidetoggle div:focus, #prompts #promptsbuttons a:focus, #prompts #promptsbuttons a:hover">Menus (Focus, Hover)</option>
 <option value="#menua1:visited, #menua2:visited, #menua3:visited, #menua4:visited, #menua5:visited, #menua6:visited, #menua7:visited, #menua8:visited, #menua9:visited, a.menuaa:visited, a.tools:visited, #toola1:visited, #toola2:visited, div.list div:visited, #sidetoggle a:visited, #sidetoggle div:visited, #prompts #promptsbuttons div a:visited, #prompts #promptstabs div a:visited">Menus (Visited)</option>
 <option value="#bottom div a">Music Player</option>
 <option value="#bottom div a:hover, #bottom div a:focus">Music Player (Hover, Focus)</option>
 <option value="form">Forms</option>
 <option value="form fieldset input.button, form fieldset input.text, form fieldset input.url, select, form fieldset textarea">Forms Fields (Buttons, Text)</option>
 <option value="form fieldset input.button:hover, form fieldset input.text:hover, form fieldset input.url:hover, select:hover, form fieldset textarea:hover, form fieldset input.button:focus, form fieldset input.text:focus, form fieldset input.url:focus, select:focus, form fieldset textarea">Forms Fields (Focus, Hover)</option>
 <option value="form fieldset label">Form Labels</option>
 <option value="ol, ul">Lists (Ordered, Unordered)</option>
 <option value="code">Code (Computer Code)</option>
 <option value="#head #location a:link, #head #location a:visited, .color1">Color 1 (Location)</option>
 <option value="#content span.subtitle, .color2">Color 2 (Subtitle)</option>
 <option value="p, p.noindent, ol li span, ul li span, .color3">Color 3 (Lists, Paragraph, Span)</option>
 <option value=".color4">Color 4 (Dashes)</option>
 <option value=".color5">Color 5 (Parenthesis, Quotes)</option>
</select>
<br style="clear: both;" />
<input id="ce00" name="ce00" type="text" value="" />
<input id="ce01" name="ce01" type="text" value="" />
<input id="ce02" name="ce02" type="text" value="" />
<input id="ce03" name="ce03" type="text" value="" />
<input id="ce04" name="ce04" type="text" value="" />
<br />
<input id="ce10" name="ce10" type="text" value="" />
<input id="ce11" name="ce11" type="text" value="" />
<input id="ce12" name="ce12" type="text" value="" />
<input id="ce13" name="ce13" type="text" value="" />
<input id="ce14" name="ce14" type="text" value="" />
<br />
<input id="ce20" name="ce20" type="text" value="" />
<input id="ce21" name="ce21" type="text" value="" />
<input id="ce22" name="ce22" type="text" value="" />
<input id="ce23" name="ce23" type="text" value="" />
<input id="ce24" name="ce24" type="text" value="" />
<br />
<input id="ce30" name="ce30" type="text" value="" />
<input id="ce31" name="ce31" type="text" value="" />
<input id="ce32" name="ce32" type="text" value="" />
<input id="ce33" name="ce33" type="text" value="" />
<input id="ce34" name="ce34" type="text" value="" />
<br />
<input id="ce40" name="ce40" type="text" value="" />
<input id="ce41" name="ce41" type="text" value="" />
<input id="ce42" name="ce42" type="text" value="" />
<input id="ce43" name="ce43" type="text" value="" />
<input id="ce44" name="ce44" type="text" value="" />
<br />
<input id="ce50" name="ce50" type="text" value="" />
<input id="ce51" name="ce51" type="text" value="" />
<input id="ce52" name="ce52" type="text" value="" />
<input id="ce53" name="ce53" type="text" value="" />
<input id="ce54" name="ce54" type="text" value="" />
<br />
<input id="ce60" name="ce60" type="text" value="" />
<input id="ce61" name="ce61" type="text" value="" />
<input id="ce62" name="ce62" type="text" value="" />
<input id="ce63" name="ce63" type="text" value="" />
<input id="ce64" name="ce64" type="text" value="" />
<br />
<input id="ce70" name="ce70" type="text" value="" />
<input id="ce71" name="ce71" type="text" value="" />
<input id="ce72" name="ce72" type="text" value="" />
<input id="ce73" name="ce73" type="text" value="" />
<input id="ce74" name="ce74" type="text" value="" />
<br />
<input id="ce80" name="ce80" type="text" value="" />
<input id="ce81" name="ce81" type="text" value="" />
<input id="ce82" name="ce82" type="text" value="" />
<input id="ce83" name="ce83" type="text" value="" />
<input id="ce84" name="ce84" type="text" value="" />
<br />
<input id="ce90" name="ce90" type="text" value="" />
<input id="ce91" name="ce91" type="text" value="" />
<input id="ce92" name="ce92" type="text" value="" />
<input id="ce93" name="ce93" type="text" value="" />
<input id="ce94" name="ce94" type="text" value="" />
<br />
<input id="ce100" name="ce100" type="text" value="" />
<input id="ce101" name="ce101" type="text" value="" />
<input id="ce102" name="ce102" type="text" value="" />
<input id="ce103" name="ce103" type="text" value="" />
<input id="ce104" name="ce104" type="text" value="" />
<br />
<input id="ce110" name="ce110" type="text" value="" />
<input id="ce111" name="ce111" type="text" value="" />
<input id="ce112" name="ce112" type="text" value="" />
<input id="ce113" name="ce113" type="text" value="" />
<input id="ce114" name="ce114" type="text" value="" />
<br />
<input id="ce120" name="ce120" type="text" value="" />
<input id="ce121" name="ce121" type="text" value="" />
<input id="ce122" name="ce122" type="text" value="" />
<input id="ce123" name="ce123" type="text" value="" />
<input id="ce124" name="ce124" type="text" value="" />
<br />
<input id="ce130" name="ce130" type="text" value="" />
<input id="ce131" name="ce131" type="text" value="" />
<input id="ce132" name="ce132" type="text" value="" />
<input id="ce133" name="ce133" type="text" value="" />
<input id="ce134" name="ce134" type="text" value="" />
<br />
<input id="ce140" name="ce140" type="text" value="" />
<input id="ce141" name="ce141" type="text" value="" />
<input id="ce142" name="ce142" type="text" value="" />
<input id="ce143" name="ce143" type="text" value="" />
<input id="ce144" name="ce144" type="text" value="" />
<br />
<input id="ce150" name="ce150" type="text" value="" />
<input id="ce151" name="ce151" type="text" value="" />
<input id="ce152" name="ce152" type="text" value="" />
<input id="ce153" name="ce153" type="text" value="" />
<input id="ce154" name="ce154" type="text" value="" />
<br />
<input id="ce160" name="ce160" type="text" value="" />
<input id="ce161" name="ce161" type="text" value="" />
<input id="ce162" name="ce162" type="text" value="" />
<input id="ce163" name="ce163" type="text" value="" />
<input id="ce164" name="ce164" type="text" value="" />
<br />
<input id="ce170" name="ce170" type="text" value="" />
<input id="ce171" name="ce171" type="text" value="" />
<input id="ce172" name="ce172" type="text" value="" />
<input id="ce173" name="ce173" type="text" value="" />
<input id="ce174" name="ce174" type="text" value="" />
<br />
<input id="ce180" name="ce180" type="text" value="" />
<input id="ce181" name="ce181" type="text" value="" />
<input id="ce182" name="ce182" type="text" value="" />
<input id="ce183" name="ce183" type="text" value="" />
<input id="ce184" name="ce184" type="text" value="" />
<br />
<input id="ce190" name="ce190" type="text" value="" />
<input id="ce191" name="ce191" type="text" value="" />
<input id="ce192" name="ce192" type="text" value="" />
<input id="ce193" name="ce193" type="text" value="" />
<input id="ce194" name="ce194" type="text" value="" />
<br />
<input id="ce200" name="ce200" type="text" value="" />
<input id="ce201" name="ce201" type="text" value="" />
<input id="ce202" name="ce202" type="text" value="" />
<input id="ce203" name="ce203" type="text" value="" />
<input id="ce204" name="ce204" type="text" value="" />
<br />
<input id="ce210" name="ce210" type="text" value="" />
<input id="ce211" name="ce211" type="text" value="" />
<input id="ce212" name="ce212" type="text" value="" />
<input id="ce213" name="ce213" type="text" value="" />
<input id="ce214" name="ce214" type="text" value="" />
<br />
<input id="ce220" name="ce220" type="text" value="" />
<input id="ce221" name="ce221" type="text" value="" />
<input id="ce222" name="ce222" type="text" value="" />
<input id="ce223" name="ce223" type="text" value="" />
<input id="ce224" name="ce224" type="text" value="" />
<br />
<input id="ce230" name="ce230" type="text" value="" />
<input id="ce231" name="ce231" type="text" value="" />
<input id="ce232" name="ce232" type="text" value="" />
<input id="ce233" name="ce233" type="text" value="" />
<input id="ce234" name="ce234" type="text" value="" />
<br />
<input id="ce240" name="ce240" type="text" value="" />
<input id="ce241" name="ce241" type="text" value="" />
<input id="ce242" name="ce242" type="text" value="" />
<input id="ce243" name="ce243" type="text" value="" />
<input id="ce244" name="ce244" type="text" value="" />
<br />
<input id="ce250" name="ce250" type="text" value="" />
<input id="ce251" name="ce251" type="text" value="" />
<input id="ce252" name="ce252" type="text" value="" />
<input id="ce253" name="ce253" type="text" value="" />
<input id="ce254" name="ce254" type="text" value="" />
</fieldset>
</form>
 
<div>
<?php
$pieces = '#welcome, #content div.border, #content div.bordernorc, #prompts div.scroll, #prompts #promptstabs div.current {background-color: #96c; background-image: url(general/11/005.gif); border-color: #06c; color: #f90;}#body, #prompts {background-color: #ccf; background-image: url(general/11/003.gif); border-color: #c9c; color: #c6c;}body, html {background-color: #c96; background-image: url(general/11/001.gif); border-color: #cc0; color: #0cf;}';
//$pieces = explode('}', $_SESSION['css']);
$number = '0';
foreach($pieces as $key=>$value)
{
$my_var1 = explode(' {', $value);
 
// Echo selector as JavaScript ID.
echo "window['ii".$key."0'] = '".$my_var1[0].'\';<br />';
 
// Echo selector's array of properties and values.
echo "window['ii".$key."1'] = '".$my_var1[1]."';<br />";
 
// Separate property/value 'sets'.
$my_var2 = explode(';', $my_var1[1]);
 
//$rules_backgroundcolor = substr($my_var2[0], strpos($my_var2[0], '{') + 19, strpos($my_var2[0], '}') - (strpos($my_var2[0], '{')-3));
//echo 'var ii'.$key.'1 = \''.$rules_backgroundcolor.'\';<br/ >';
 
//$rule_backgroundimage = substr($my_var2[1], strpos($my_var2[1], '{') + 31, strpos($my_var2[1], '}') - (strpos($my_var2[1], '{')-6));
//echo 'var ii'.$key.'2 = \''.$rule_backgroundimage.'\';<br/ >';
 
//$rule_bordercolor = substr($my_var2[2], strpos($my_var2[2], '{') + 16, strpos($my_var2[2], '}') - (strpos($my_var2[2], '{')-3));
//echo 'var ii'.$key.'3 = \''.$rule_bordercolor.'\';<br/ >';
 
//$rule_color = substr($my_var2[3], strpos($my_var2[3], '{') + 9, strpos($my_var2[3], '}') - (strpos($my_var2[3], '{')-3));
//echo 'var ii'.$key.'4 = \''.$rule_color.'\';<br/ >';
echo '<br />';
}
?>
</div>
 
</body>
</html>

Re: Split string and retain part before character match?

Posted: Mon Jun 16, 2008 4:14 pm
by Ambush Commander
Your explanation still gets too hung up on implementation details. Let's say I was a end-user who paid the big bucks for this feature: what does it do for me? (the reason I ask is because I still have no clue what you're talking about)