odd explode( function needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xion.truth
Forum Newbie
Posts: 4
Joined: Wed Jul 01, 2009 3:33 pm

odd explode( function needed

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: odd explode( function needed

Post 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[] ?)
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: odd explode( function needed

Post by BornForCode »

So what you need is to split the string into chunks of two characters, right?
xion.truth
Forum Newbie
Posts: 4
Joined: Wed Jul 01, 2009 3:33 pm

Re: odd explode( function needed

Post 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
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: odd explode( function needed

Post by BornForCode »

Probably something like: preg_split('([a-zA-Z][0-9])',$string)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: odd explode( function needed

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 1:57 pm, edited 1 time in total.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: odd explode( function needed

Post 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!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: odd explode( function needed

Post 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:
xion.truth
Forum Newbie
Posts: 4
Joined: Wed Jul 01, 2009 3:33 pm

Re: odd explode( function needed

Post 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!!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: odd explode( function needed

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: odd explode( function needed

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: odd explode( function needed

Post by McInfo »

xion.truth wrote:all check boxes are predefined as a-z1-9
Edit: This post was recovered from search engine cache.
Post Reply