0

I have a code like this to find a pattern for example ##random_number_10## I need to take the number 10 to be used as a parameter in the function and that function will go to str_replace to replace the pattern

string :

ini subjek lur ##random_mix_5## ##random_string_up_10## ##random_number_15##

output :

ini subjek lur o1ovp HDBVPVJTYY 741007211142434

.

function replace ($sumber) {

        preg_match_all("/##random_number_(\d+)##/", $sumber, $match_number);
        preg_match_all("/##random_string_up_(\d+)##/", $sumber, $match_string_up);
        preg_match_all("/##random_string_low_(\d+)##/", $sumber, $match_string_low);
        preg_match_all("/##random_string_uplow_(\d+)##/", $sumber, $match_string_uplow);
        preg_match_all("/##random_mix_(\d+)##/", $sumber, $match_mix);

        $dari = [
            $match_number[0][0], 
            $match_string_up[0][0], 
            $match_string_low[0][0], 
            $match_string_uplow[0][0], 
            $match_mix[0][0]
        ];

        $ke   = [
            random_number($match_number[1][0]), 
            random_string_up($match_string_up[1][0]), 
            random_string_low($match_string_low[1][0]), 
            random_string_uplow($match_string_uplow[1][0]), 
            random_mix($match_mix[1][0])
        ];

        return str_replace($dari, $ke, $sumber);

    };

the problem is when I try to replace all of them an error will appear if the string does not contain one of the searches. is there another way to replace the pattern on the string?

2 Answers 2

1

You're probably getting an Undefined offset notice because you're referencing an array key that doesn't exist:

$sumber = "ini subjek lur ##random_mix_5## ##random_string_up_10## ##random_number_15##";

preg_match_all("/##random_number_(\d+)##/", $sumber, $match_number);
preg_match_all("/##random_string_up_(\d+)##/", $sumber, $match_string_up);
preg_match_all("/##random_string_low_(\d+)##/", $sumber, $match_string_low);
preg_match_all("/##random_string_uplow_(\d+)##/", $sumber, $match_string_uplow);
preg_match_all("/##random_mix_(\d+)##/", $sumber, $match_mix);

var_dump($match_string_uplow[1][0]);

Will output: Notice: Undefined offset: 0 on line 11

A slightly hacky solution is to add a @ symbol before each value being sent. This will surpress the error message and simply return a NULL value:

var_dump(@$match_string_uplow[1][0]);

Will output: NULL

Now it could also be that your error is generated by the function you're calling if you don't submit it a value. In which case, the solution will depend on how strict your function is when defining values.

Sign up to request clarification or add additional context in comments.

2 Comments

@MukhlisAkbarrudin: Hide an error message is never a good idea.
@Toto While I completely agree with you in principle, in this particular context, all you're hiding is an expected undefined offset notice. Frankly, most people turn off all notice level errors anyway, so it's not a huge crime to hide it here. However, I agree that it's not a good idea to make a habit out of the @ prefix.
1

Build the 2 arrays only if there is a match:

function replace ($sumber) {
    $dari = array();
    $ke = array();
    if (preg_match_all("/##random_number_(\d+)##/", $sumber, $match)) {
        $dari[] = $match[0];
        $ke[]   = $match[1]
    }
    if (preg_match_all("/##random_string_up_(\d+)##/", $sumber, $match)) {
        $dari[] = $match[0];
        $ke[]   = $match[1]
    }
    if (preg_match_all("/##random_string_low_(\d+)##/", $sumber, $match)) {
        $dari[] = $match[0];
        $ke[]   = $match[1]
    }
    if (preg_match_all("/##random_string_uplow_(\d+)##/", $sumber, $match)) {
        $dari[] = $match[0];
        $ke[]   = $match[1]
    }
    if (preg_match_all("/##random_mix_(\d+)##/", $sumber, $match)) {
        $dari[] = $match[0];
        $ke[]   = $match[1]
    }

    return str_replace($dari, $ke, $sumber);

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.