Can't display Preg_match return array.

Published 3 weeks ago by klik

Hi. I created new helper file and added function that using preg_match to check string.

In HTML:

{{commentAnswerTo($comment->content)}}

In helper:

function commentAnswerTo(string $text)
{
    preg_match("/[|][a-z0-9_ąśżźćńłóę]+[|]/i",$text,$match);
    var_dump($match);   
}

As a result I have:

array(1) { [0]=> string(7) "|Usuri|" }

And result is absolutely correct.

But when I try get value from array:

echo $match[0];

then I have error:

Undefined offset: 0 (View: \resources\views\post\view.blade.php).

Why I can't get this value?

I did a test adding string in function:

function commentAnswerTo(string $text)
{
    $test = '|Usuri| This is a random text in comment'  

    preg_match("/[|][a-z0-9_ąśżźćńłóę]+[|]/i",$test,$match);
    echo $match[0];   
}

And then I can get a value from array. So why I can't when I pass string as a function parameter?

Thank you.

Best Answer (As Selected By klik)
Cronix

But when I try get value from array: echo $match[0];

How are you doing that? It's hard to tell what your actual code is and what your test code is. In your commentAnswerTo function, you're using var_dump instead of returning anything, so I'm not sure where/how you are trying to access $match[0]...

function commentAnswerTo(string $text)
{
    preg_match("/[|][a-z0-9_ąśżźćńłóę]+[|]/i",$text,$match);
    return $match[0] ?? '';
}

Does that work?

Cronix
Cronix
3 weeks ago (473,740 XP)

But when I try get value from array: echo $match[0];

How are you doing that? It's hard to tell what your actual code is and what your test code is. In your commentAnswerTo function, you're using var_dump instead of returning anything, so I'm not sure where/how you are trying to access $match[0]...

function commentAnswerTo(string $text)
{
    preg_match("/[|][a-z0-9_ąśżźćńłóę]+[|]/i",$text,$match);
    return $match[0] ?? '';
}

Does that work?

klik

Hi. WTF?

This one works.

return $match[0] ?? '';

But this one doesn't.

return $match[0];

But what is a difference? In both cases match[0] exist, so why in your example I can return value but no when do return $match[0]?

Thank you.

Cronix
Cronix
3 weeks ago (473,740 XP)

Couldn't really say if you're sure that $match[0] exists in every case. I just put it there to return an empty string in case $match[0] didn't exist.

Cronix
Cronix
3 weeks ago (473,740 XP)

@klik If it's working for you now, please mark as solved.

klik

That match[0] exists in every case. Now I did a test using explode method and I have the same situation. Index dosn't exist but var_dump() shows it exists. Is it a bug?

Cronix
Cronix
3 weeks ago (473,740 XP)

If it's a bug, it's a bug in php, not laravel, since laravel isn't even being used in that function, assuming your function looks like:

function commentAnswerTo(string $text)
{
    preg_match("/[|][a-z0-9_ąśżźćńłóę]+[|]/i",$text,$match);
    return $match[0];
}

But it's never a good idea to assume it will always exist. It's better to return some default if it doesn't so at least it won't error out.

this would also work, but I like the null coalesce operator better.

function commentAnswerTo(string $text)
{
    if (preg_match("/[|][a-z0-9_ąśżźćńłóę]+[|]/i",$text,$match)) {
        return $match[0];
    }

    return '';
}
klik

I know, it's only test code. Thx.

Please sign in or create an account to participate in this conversation.