I have recently started looking into using functions as for the first time, I have some repetitive code. However after reading up on functions and understanding the basics I am unsure whether what I am after is possible. Basically:
I have five variables:
$message1, $message2, $message3, $message4, $message5
These contain formatted messages with paragraphs, bolden, etc. What I want to do is get the first paragraph of each variable and then decode, to display nicely. What I have currently is:
$message1 = preg_split('/((<\s*p\s*\/?>\s*)|(<\s*br\s*\/?>\s*)|(\s\s+)|(<\s*\/p\s*\/?>\s*))+/', $message1, -1, PREG_SPLIT_NO_EMPTY);
echo htmlspecialchars_decode($message1[0]);
(Obviously in a function I wouldn’t be using echo). But I am using this piece of code for each variable. I am trying to create a function that allows me to pass each variable through and then return the variable back to use. I have looked around and read up lots on functions but I think I have got to a point where I have completely lost the plot.
My theory (which I know is wrong, but unsure where to go from) is:
function displayMessage($message) {
$message = preg_split('/((<\s*p\s*\/?>\s*)|(<\s*br\s*\/?>\s*)|(\s\s+)|(<\s*\/p\s*\/?>\s*))+/', $message, -1, PREG_SPLIT_NO_EMPTY);
$message = htmlspecialchars_decode($message[0]);
return;
}
echo displayMessage($message1);
echo displayMessage($message2);
// etc.
I know this won’t work but I have no idea how it could be done. Could someone point me in the right direction or suggest another way that I could get this done?
Thanks