$insert = "how are you today? nice to see you again. here is your login ";
$wwwcheck = $nameinput;
if(preg_match("/name1|name2|name3|name4/i", $nameinput)) {
$tpl = str_replace('name1 ', $insert . '<a href="/folder/name1/">name1</a> ', $tpl);
$tpl = str_replace('name2 ', $insert . '<a href="/folder/name2/">name2</a> ', $tpl);
$tpl = str_replace('name3 ', $insert . '<a href="/folder/name3/">name3</a> ', $tpl);
$tpl = str_replace('name4 ', $insert . '<a href="/folder/name4/">name4</a> ', $tpl);
}
which has 60 lines and is only going to keep getting bigger. Is there anyway to shrink the principal down to a single line?
e.g.
$insert = "how are you today? nice to see you again. here is your login ";
$wwwcheck = $nameinput;
if(preg_match("/name1|name2|name3|name4/i", $nameinput)) {
$tpl = str_replace('$name ', $insert . '<a href="/folder/$name/">$name</a> ', $tpl);
}
Not tested but something along the lines of the following should work:
Code:
$insert = "how are you today? nice to see you again. here is your login ";
$wwwcheck = $nameinput;
$tpl = preg_replace("/(name[1-4])/i", $insert . '<a href="/folder/$1/">$1</a> ',$nameinput);
Any way to get the URl $1 to be lowercase? I've tried sticking strtolower in there in various forms, but it just breaks the code. Is the $1 usage causing a problem doing anything else with it?
You could convert every name to lowercase before the preg_replace. One other option is to use the preg_replace_callback function (check the following code).