klik
154
7
General

Can't display Preg_match return array.

Posted 11 months 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.

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

Reply to

Use Markdown with GitHub-flavored code blocks.