klik

Can't display Preg_match return array.

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.