Jump to content

preg_match and preg_replace help, please?


mdrisser

Recommended Posts

first a bit of context, I have a form that contains checkboxes that are declared like this:

[code]<input type="checkbox" name="zones[]" value="blah"/>[/code]

After the form is submitted I put the checkbox values into a variable like so:

[code]$i = $_POST['zones'];
while(list($key,$value)=each($i)) {
    if(preg_match('/^(Array)/',$value)) {
        $value = preg_replace('/^(Array)/','//',$value);
        echo("Replaced Array<br/>");
    }
    $zones .= $value . ",";
}[/code]

I'm trying to remove "Array" because, for whatever reason, the first element of the $i array always contains it.
i.e.
[code]$i[0] == "ArrayOceanside"[/code]

I placed the echo() in for debugging, but it never gets to it. So I assume that for whatever reason my preg_match() is returning flase, but I just can't quite figure out why.

Any help is greatly appreciated, as I'm running out of hair to pull out.  ;)

Link to comment
Share on other sites

I'm not entirely sure why you have this:
[code]while(list($key,$value)=each($i))[/code]
I'd use:
[code]foreach($i as $row)[/code]
The keys for $_POST['zones'] are just 0, 1, 2...etc.
I think the 'list' and 'each' in the 'while' (try reading that to someone that doesn't program!) are getting tripped up on each other. Your regex looks good, you don't need the parenthesizes around [code]'/^(Array)/'[/code]
And the forward slashes in the replacement value shouldn't be there, unless you want to replace "Array" with "//"

Try this:
[code]$i = $_POST['zones'];
foreach($i as $row) {
    if(preg_match('/^Array/',$row)) {
        $row = preg_replace('/^Array/', '' , $row);
        echo("Replaced Array<br/>");
    } else {
        echo("Didn't Replace Array<br />");
    }
}[/code]
Really, I don't think you'll have that problem if you use "foreach" instead of the "while" thing. Check the output of "foreach" before you code in the regex.
Link to comment
Share on other sites

c4,
Thank you very, very much.

The foreach() loop actually worked out better than I had expected. Running the foreach() negated the need for the regex at all.

The code now reads:
[code]
if($_POST['zones'] != "") {
    $zones = $_POST['zones'];
}

foreach($zones as $value) {
    echo($value . "\n");
}
[/code]

Which now outputs the array properly, i.e. without 'Array' being prepended to the first element of the array. ([i]I'm really not sure why it was happening to begin with, but I'll figure that out later I suppose, when I have more time.[/i]) You've just gotta love how changing one relatively small thing, can fix many problems 

Now its time to go get some hair plugs, so I have something to pull out next time....... ;D
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.