I can't seem to figure this out... and I admit I'm not up to date with the new stuff. I was using eregi_replace
, but switched it to preg_replace
and added the delimiter. Now it is not working correctly.
I have fields that are in a form, that are sent to a form validator, and the error message lets the user know which fields are missing; i.e. "firstname, lastname, password, etc.". The error needs to show as "First Name or Last Name", basically adding a space and capitalizing the N in Name.
Can someone point out what I'm doing wrong?
This is what used to work:
$r .= ucwords(eregi_replace("_", " ", $c));
This is what I changed it to:
$r .= ucwords(preg_replace("/_/", " ", $c));
Now they just show as Firstname
, Lastname
but I would like them to show as First Name
, Last Name
.
Hello it might be working like this:
<?php
$c = array('Firstname', 'Lastname', 'password');
foreach($c as $field_name)
{
$r = ucwords(preg_replace("/name/i", " Name", $field_name));
echo $r."<br>";
}
Your question and code didn't demonstrate what you were after.
$r .= ucwords(eregi_replace("_", " ", $c));
Never would have found name
, that is looking for an underscore and replacing it with a space.
Which is why
preg_replace("/_/", " ", $c);
didn't work for you. That is a correct conversion but the regex is solving the wrong problem. Given the answer above another approach you could've, since a regex isn't needed, would be str_ireplace.
$c = array('Firstname', 'Lastname', 'password');
foreach($c as $field_name) {
$r = ucwords(str_ireplace("name", " Name", $field_name));
echo $r."
";
}
Demo: https://eval.in/452283