I am new at php and I was trying to create a BBcode but it gives me this error:

Warning: preg_replace(): No ending delimiter '@' found

The code I did:

function bbCode($str){
    $values = array(
                    '@\[b]\(.*?)\[\/b]\@i' => '<b>$1</b>',
                    '@\[i]\(.*?)\[\/i]\@i' => '<em>$1</em>'

                    );
                    return preg_replace(array_keys($values), array_values($values), $str);
}

What is wrong with it?

有帮助吗?

解决方案

Your escaping the last @ symbol by using \@ so php is interpreting the @ as a literal @ rather than the delimiting character.

EDIT:

function bbCode($str)
{
    $values = array(
      '@\[b\](.*?)\[\/b\]@i' => '<b>$1</b>',
      '@\[i\](.*?)\[\/i\]@i' => '<em>$1</em>'

    );
    return preg_replace(array_keys($values), array_values($values), $str);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top