
|
View Full Version : Understanding a script
saghir69 04-05-2008, 04:49 PM I'm using this script in my backend system, I'm a bit confused how it works. Can someone explain this? its pretty small.
The code changes a string into name format. so "first name surename" becomes "First Name Surename"
I'm running my database info through it to namize them.
So if the data was O\'reilly the out put would be O'Reilly
which is what I want, I just want to know how?
I know this code
$string = strtolower($str);
changes the \ to " " a space, I'm just wondering which part of the code removes this space?
function nameize($str,$a_char = array("'","-"," ")){
//$str contains the complete raw name string
//$a_char is an array containing the characters we use as separators for capitalization. If you don't pass anything, there are three in there as default.
$string = strtolower($str);
foreach ($a_char as $temp){
$pos = strpos($string,$temp);
if ($pos){
//we are in the loop because we found one of the special characters in the array, so lets split it up into chunks and capitalize each one.
$mend = '';
$a_split = explode($temp,$string);
foreach ($a_split as $temp2){
//capitalize each portion of the string which was separated at a special character
$mend .= ucfirst($temp2).$temp;
}
$string = substr($mend,0,-1);
}
}
return ucfirst($string);
}
Codebird 04-05-2008, 05:41 PM function nameize($str,$a_char = array("'","-"," ")){
//$str contains the complete raw name string
//$a_char is an array containing the characters we use as separators for capitalization. If you don't pass anything, there are three in there as default.
//****Changing all the string to lower case****
$string = strtolower($str);
//****Iterating through all the special characters array****
foreach ($a_char as $temp){
//****Finding the position of the special character****
$pos = strpos($string,$temp);
//****If the position is not empty****
if ($pos){
//we are in the loop because we found one of the special characters in the array, so lets split it up into chunks and capitalize each one.
$mend = '';
//****Splitting the string on this character****
$a_split = explode($temp,$string);
//****Iterating through the array****
foreach ($a_split as $temp2){
//capitalize each portion of the string which was separated at a special character
//****putting the first character to upper case then concatenating the special character****
$mend .= ucfirst($temp2).$temp;
}
//***Setting string to the result****
$string = substr($mend,0,-1);
}
}
//****Returning the string with the first character in upper case****
return ucfirst($string);
}
the comments with **** at the beginning and end are the ones I added
etogre 04-05-2008, 05:47 PM strtolower() just lowercases the entire string, it does not change a backslash into a " " space. And actually you probably have a stripslashes() somewhere in your code that is removing the \ in O\'reilly.
The code is just finding all the occurences of your *$a_char = array("'","-"," ")*, and then breaking the string apart on that. Then it capitalizes the letter that appears after each of those "breaks". Then it puts it into a new string as it goes along and then finally spits that out at the end.
Actually, that is a pretty neat function you have. Although, I found the return line ( return ucfirst($string); ) to be redundant, you can just "return $string;" and return the same results.
Adam-AEC 04-05-2008, 05:52 PM That function looks like a glorified version of ucwords(). http://php.net/ucwords
strtolower() converts all characters to lowercase.
That function could be reduced to:
function nameize($name) {
return ucwords(preg_replace("/('\w)/e", "strtoupper('\\1')", $name));
}
Way more efficient.
Codebird 04-05-2008, 05:55 PM Although, I found the return line ( return ucfirst($string); ) to be redundant
no it is not redundant the code is assuming that the first letter will not be put to upper case cause the string can't start with a special character so at the end it is returning the string with the first letter to upper
etogre 04-05-2008, 05:56 PM Test it with just returning a string starting with a lowercase letter and it works.
/shrug
Codebird 04-05-2008, 05:59 PM yes for sure it will work well cause u started with a special character
Codebird 04-05-2008, 06:22 PM oh you're right etogre I am sorry, I relooked at the code one more time. thanks
saghir69 04-06-2008, 01:44 AM This is the code I'm using to get the data from my table and then update the table.
there is not stripslashes in the code, why is my slash disappearing?
do you think its magic quotes that is doing it?
$result = mysql_query("SELECT * FROM table where field = '0' ORDER BY id DESC LIMIT 30") ;
while($row = mysql_fetch_array( $result )) {
$cfield=nameize($row['field']);
$id = $row['id'];
$query="UPDATE table SET field='$cfield'";
mysql_query($query);
Burhan 04-06-2008, 02:11 AM Your code, specifically this part:
$query = "UPDATE table SET field='$cfield'";
mysql_query($query);
Is updating the entire table (all rows).
saghir69 04-06-2008, 02:15 AM Your code, specifically this part:
$query = "UPDATE table SET field='$cfield'";
mysql_query($query);
Is updating the entire table (all rows).
I wrote field just as example.
each field has a name.
so the code would be
$query = "UPDATE table SET namefield='$cfield'";
mysql_query($query);
saghir69 04-06-2008, 06:31 AM This is the code I'm using to get the data from my table and then update the table.
there is not stripslashes in the code, why is my slash disappearing?
do you think its magic quotes that is doing it?
$result = mysql_query("SELECT * FROM table where field = '0' ORDER BY id DESC LIMIT 30") ;
while($row = mysql_fetch_array( $result )) {
$cfield=nameize($row['field']);
$id = $row['id'];
$query="UPDATE table SET field='$cfield'";
mysql_query($query);
i'm using this code
$id = $row['id'];
$query="UPDATE table SET field='$cfield' WHERE id='$id'";
mysql_query($query);
I'm just wondering where the backslash is taken off?
|