Welcome Guest, Not a member yet? Register   Sign In
Check part of string is in_array()
#1

[eluser]codex[/eluser]
Is it possible to check whether if only a part of a string is present in an array?

See, i need to check if 'min_length[8]' is in_array, but since the number is variable I can't check the whole string, so I just need to know if 'min_length' is found.

Also, I need to return the value between the brackets ([]). Is there a PHP function lets me get 'anything between char [ and char ]'? Or is this requiring regex stuff?
#2

[eluser]bradym[/eluser]
That's very similar to the validation class in CI - have you looked at system/libraries/Validation.php to see how it's done there?
#3

[eluser]codex[/eluser]
[quote author="bradym" date="1211698791"]That's very similar to the validation class in CI - have you looked at system/libraries/Validation.php to see how it's done there?[/quote]

Indeed, it's the same thing I'm looking for. Should have thought about that. But hey, it's late (well, over here it is) ;-)

Thanks again!

Edit: Hmm, I spoke too soon. It's not what I need, though it comes in handy in another spot. Basically I need to find 'min_length[anything_here]'. Sorta like a wildcard.

Edit2: Come to think about it, it IS what I need. I just looked at it the wrong way.
#4

[eluser]codex[/eluser]
I thought the function in Validation.php would help with what I'm trying to achieve, but no luck yet. I may be on the wrong track so maybe there's someone else that sees a better solution.

I'm creating a module for dynamic form creation. Through the admin you can add and edit inputs of all sorts. The adding/editing also involves settings for validation, which is being written to the db as one string, just as it's done with regular validation settings: required|min_length[2]|alpha_numeric

To edit the input values I explode the validation string, check if the rule is in the array, and write the corresponding values to the 'edit'-form on screen:

Code:
# Any rules found?
            if (! empty($data['input'][0]->input_validation_rules)) // This is the validation string
            {
                $ex = explode('|', $data['input'][0]->input_validation_rules);
                
                # Required
                $data['required'] = in_array('required', $ex) ? 1 : ''; // Works
                
                # Alpha
                if (in_array('alpha', $ex))
                {
                    $data['allowed_chars'] = 'alpha'; // Works
                }
                else if (in_array('alpha_numeric', $ex))
                {
                    $data['allowed_chars'] = 'alpha_numeric'; // Works
                }
                else if (in_array('alpha_dash', $ex))
                {
                    $data['allowed_chars'] = 'alpha_dash'; // Works
                }
                
                # Min length
                if (in_array('min_length[*]', $ex)) // This obviously won't work, but can you wildcard a string in php?
                {
                    // The parameter needs to be set here
                    // $data['min_length'] = $param;
                }
            }
#5

[eluser]Derek Allard[/eluser]
lol, are the voices in your head fighting again? I know exactly how you feel Codex.
#6

[eluser]codex[/eluser]
[quote author="Derek Allard" date="1211737086"]lol, are the voices in your head fighting again? I know exactly how you feel Codex.[/quote]

I'm not sure I understand what you are saying, but do you mean that one moment I say I have it solved and the other moment I haven't?
#7

[eluser]Derek Allard[/eluser]
Yeah, it was just a joke. In post #2 you seem to have a conversation with yourself.
#8

[eluser]codex[/eluser]
[quote author="Derek Allard" date="1211737545"]Yeah, it was just a joke. In post #2 you seem to have a conversation with yourself.[/quote]

Heheh, yeah Tongue

But, what I did in #2, isn't that exactly what those folks at Twitter are doing?
#9

[eluser]bradym[/eluser]
This may not be the best way to do it, but here's some quick code for you:

Code:
<?
$string = 'required|min_length[2]|alpha_numeric';

$rules_array = explode('|',$string);

// Let's take a look at the array
echo '<pre>';print_r($rules_array);echo '</pre>';

foreach($rules_array as $rule)
{
    $start_num = strpos($rule,'[');
    $end_num = strpos($rule,']');
    
    if($start_num !== FALSE)
    {
        // Adjust $start_num since we don't want to include the square brackets
        $start_num += 1;
        $len = $end_num - $start_num;
        $num = substr($rule,$start_num,$len);

        // Display the number
        echo $num;
    }
}
?&gt;

Edit: Looking at your code again, I don't think this is quite what you're looking for.. oops




Theme © iAndrew 2016 - Forum software by © MyBB