Connecting Tech Pros Worldwide Forums | Help | Site Map

php pattern for text (if there is any dot found set a single space for next word!)

Member
 
Join Date: Feb 2007
Location: malaysia
Posts: 51
#1: Feb 28 '07
Here is my code:

if(isset($_REQUEST['Submit']))
{
//capture name from textfield
$name = $_POST['name'];

//make pattern for name
function checkName($uname)
{
$pattname = "/^[a-z\.\s]+$/";
return preg_match($pattname,$uname);
}

if(!checkName($nama))
{
//display error for invalid name
echo "$nama invalid name!";
}
else
{
//display formated name if name is valid
echo "Your name: ".ucwords(strtolower(wordwrap($nama)))." (after formated!)";
}
}

My problem is i can't make a pattern for short name like "M.Micheal" where if there is any dot(.) found set a single space for next word after dot (M. <space>Micheal OR M. Micheal). Is that possible to using ereg_replace() function?

Member
 
Join Date: Feb 2007
Location: malaysia
Posts: 51
#2: Feb 28 '07

re: php pattern for text (if there is any dot found set a single space for next word!)


I have been completed, here is the solution...

//just add str_replace() function like below
echo "Your name: ".ucwords(strtolower(wordwrap(str_replace(".", ". ",$name))))." (after formated!)";
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#3: Feb 28 '07

re: php pattern for text (if there is any dot found set a single space for next word!)


When you want people to help you, enclose your shown code within php or code tags!!

Read the Posting Guidelines before you show any more code in this forum.

moderator
Reply