Page 1 of 1

odd explode( function needed

Posted: Wed Jul 01, 2009 3:49 pm
by xion.truth
if any one can help with this it would be much appreciated:

I have a string that looks something like this but longer

Code: Select all

$String = a3a8b2a4c9z8a9;
What i need to do is seperate them into an array so thay look like this:

Code: Select all

 
$a3 =$String[]; //a3 from array
$a8=$String[];//a8 from array
$b2=$String[];//b2 from array
$a4=$String[];//a4 from array
$c9=$String[];//c9 from array
$z8=$String[];//z8 from array
$a=$String[]';//9a9 from array
//There are over 250 possible combinations aka a1 a2 a3 ... a9 b1 b2...b9 .... z9
 
each value represents a text box defined as

Code: Select all

 
  <tr>
      <td><div align="left">
        <input name="a3" type="checkbox" id="a3" value="1" <?php echo $a3; ?> />
        name of check box X# of 250</div></td>
  </tr>
  <tr>
      <td><div align="left">
        <input name="a8" type="checkbox" id="a8" value="1" <?php echo $a8; ?> />
        name of check box X# of 250</div></td>
  </tr>
  <tr>
      <td><div align="left">
        <input name="b2" type="checkbox" id="b2" value="1" <?php echo $b2; ?> />
        name of check box X# of 250</div></td>
  </tr>
  <tr>
      <td><div align="left">
        <input name="a4" type="checkbox" id="a4" value="1" <?php echo $a4; ?> />
        name of check box X# of 250</div></td>
  </tr>
  <tr>
      <td><div align="left">
        <input name="c3" type="checkbox" id="c3" value="1" <?php echo $c9; ?> />
        name of check box X# of 250</div></td>
  </tr>
  <tr>
      <td><div align="left">
        <input name="z8" type="checkbox" id="z8" value="1" <?php echo $z8; ?> />
        name of check box X# of 250</div></td>
  </tr>
  <tr>
      <td><div align="left">
        <input name="a9" type="checkbox" id="a9" value="1" <?php echo $a9; ?> />
        name of check box X# of 250</div></td>
  </tr>
 
 
all check boxes are predefined as a-z1-9

Thanks in advance

Re: odd explode( function needed

Posted: Wed Jul 01, 2009 3:52 pm
by Darhazer
You can use preg_split for that, but I do not understand what you should have at the end (what means $a8 = $String[] ?)

Re: odd explode( function needed

Posted: Wed Jul 01, 2009 3:56 pm
by BornForCode
So what you need is to split the string into chunks of two characters, right?

Re: odd explode( function needed

Posted: Wed Jul 01, 2009 4:06 pm
by xion.truth
Yes I didn't even think about that. I have never used preg_split would you mind giving the syntax

as for the "$a8=$String[];//a8 from array" i was not explaining myself well, i was just wanting to creat a variable called $a8 and assign it to the array value of a8

I will dynamically setting the checkbox to checked or unchecked so if

$a8 == ''; then that box will not be checked but if $a8 == 'a8'; then it will be checked

thank you very much

Re: odd explode( function needed

Posted: Wed Jul 01, 2009 4:20 pm
by BornForCode
Probably something like: preg_split('([a-zA-Z][0-9])',$string)

Re: odd explode( function needed

Posted: Thu Jul 02, 2009 12:11 am
by McInfo
Actually, preg_split() is not the appropriate function for this. The appropriate regular expression function would be preg_match_all(), but regular expressions are overkill for splitting a string into fixed-length chunks. Use str_split().

Code: Select all

<?php
header('Content-Type: text/plain');
$str = 'a1b2c3d4e5f6';
$pairs = str_split($str, 2);
print_r($pairs);
?>

Code: Select all

Array
(
    [0] => a1
    [1] => b2
    [2] => c3
    [3] => d4
    [4] => e5
    [5] => f6
)
Edit: This post was recovered from search engine cache.

Re: odd explode( function needed

Posted: Thu Jul 02, 2009 5:56 am
by prometheuzz
McInfo wrote:Actually, preg_split() is not the appropriate function for this. The appropriate regular expression function would be preg_match_all(), ...
preg_match_all and preg_split are both as inappropriate if you ask me:

Code: Select all

$str = 'a1b2c3d4e5f6';
$pairs = preg_split('/(?<!^)(?=[a-z]\d)/', $str);
print_r($pairs);
McInfo wrote:but regular expressions are overkill for splitting a string into fixed-length chunks. Use str_split().
...
I couldn't agree more!

Re: odd explode( function needed

Posted: Thu Jul 02, 2009 5:57 am
by prometheuzz
BornForCode wrote:Probably something like: preg_split('([a-zA-Z][0-9])',$string)
If you'd try, you'd know that would not work.
BornForCode wrote:...
Enjoyed my solution? Buy me a beer.
And if I didn't like your answer, do you buy me a beer? :wink:

Re: odd explode( function needed

Posted: Thu Jul 02, 2009 8:13 am
by xion.truth
prometheuzz wrote:
McInfo wrote:Actually, preg_split() is not the appropriate function for this. The appropriate regular expression function would be preg_match_all(), ...
preg_match_all and preg_split are both as inappropriate if you ask me:

Code: Select all

$str = 'a1b2c3d4e5f6';
$pairs = preg_split('/(?<!^)(?=[a-z]\d)/', $str);
print_r($pairs);
McInfo wrote:but regular expressions are overkill for splitting a string into fixed-length chunks. Use str_split().
...
I couldn't agree more!
That solved the problem

Thank you so much!!

Re: odd explode( function needed

Posted: Thu Jul 02, 2009 8:42 am
by prometheuzz
xion.truth wrote:
prometheuzz wrote:
McInfo wrote:Actually, preg_split() is not the appropriate function for this. The appropriate regular expression function would be preg_match_all(), ...
preg_match_all and preg_split are both as inappropriate if you ask me:

Code: Select all

$str = 'a1b2c3d4e5f6';
$pairs = preg_split('/(?<!^)(?=[a-z]\d)/', $str);
print_r($pairs);
McInfo wrote:but regular expressions are overkill for splitting a string into fixed-length chunks. Use str_split().
...
I couldn't agree more!
That solved the problem

Thank you so much!!
Why do you want to do it using preg_split? In this case, McInfo's suggestion is better, IMO.

Re: odd explode( function needed

Posted: Fri Jul 03, 2009 1:10 am
by Darhazer
prometheuzz wrote:
Why do you want to do it using preg_split? In this case, McInfo's suggestion is better, IMO.
But if there is a10, it won't be fixed-width any more

Re: odd explode( function needed

Posted: Fri Jul 03, 2009 1:27 am
by McInfo
xion.truth wrote:all check boxes are predefined as a-z1-9
Edit: This post was recovered from search engine cache.