Let's say we want to uppercase the first letter in some strings like :
johndev
johnasp
johnphp
johnserver
and we use this for such purpose :
ucfirst(str_replace($name,ucfirst($name),$result['#attributes']['overflows']))
john is our $name variable. It works like this :
Johndev //** these also should be in uppercase, for example : JohnDev
Johnasp
Johnphp
Johnserver
DevJohn
AspJohn
PhpJohn
ServerJohn
How can I fix this?
My solution is:
echo
ucfirst($name)
. ucfirst(substr($result['#attributes']['overflows'], strlen($name)));
Here what you do:
ucfirst $name itself
then ucfirst part of $result['#attributes']['overflows'] which comes after $name
concatenate both parts
For strings like 'PhpJohn' just swap parts:
echo
ucfirst(substr($result['#attributes']['overflows'], strlen($name)))
. ucfirst($name);
Assuming you have $whole and $john, and $name is always the prefix of $whole.
$whole = "johndev";
$name = "john";
$capName = ucfirst($name); // "John"
$tail = substr($whole, strlen($name)); // "dev"
$capTail = ucfirst($tail); // "Dev"
echo $capName . $capTail; // "JohnDev"
If $name could appear anywhere in $whole for any number of times, then you can use:
$whole = "phpjohndevjohnjava";
$name = "john";
$parts = explode($name, $whole); // ["php", "dev", "java"]
$capParts = array_map(ucfirst, $parts); // ["Php", "Dev", "Java"]
$capName = ucfirst($name); // "John"
$answer = implode($capName, $capParts); // "PhpJohnDevJohnJava"
echo $answer;
Finally, I found a way to fix this issue :
$thekey = str_replace($name, '', $result['#attributes']['overflow']);
$ucname = str_replace($name,ucfirst($name),$result['#attributes']['overflow']);
$thename = str_replace($thekey,ucfirst($thekey),$ucname);
echo ucfirst($thename);
Related
I'm struggling with the following script:
$filename = '../lang.nl.php';
$string = '<?php // language = ' . $filename . '<br>';
foreach ($_POST as $param_name => $param_val) {
$string .= "$lang=['". $param_name ."'] = ". $param_val .";\n";
}
$string .= "?>";
file_put_contents($filename, $string);
As you can see I want to create a language file with all the $_POST variables but PHP sees the $lang in $string as a variable. You can imagine that this is not what I want, it should just print $lang not whatever $lang as a variable should be. I get the error that $lang doesn't exist but I just want to literally print $lang.
Escape the $: $string .= "\$lang=['". $param_name ."'] = ". $param_val .";\n";
What is different from the original code is the backslash before $lang, which makes the $ sign an ordinary $ sign and not a marker for a variable name.
It's easier (and safer) to use var_export here:
$filename = '../lang.nl.php';
$post = var_export($_POST, true);
$code = "<?php // language = $filename;
\$lang = $post;
?>";
file_put_contents($filename, $code);
"$lang=['". $param_name ."'] = ". $param_val; is going to fail if param_val contains a quote.
I wish to have one string that contains for instance (mystring):
file config.php
$mystring = "hello my name is $name and i got to $school";
file index.php
include('config.php');
$name = $_GET['name'];
$school = $_GET['school'];
echo $mystring;
Would this work ? or are there any better ways
$string = 'Hello, my name is %s and I go to %s';
printf($string, $_GET['name'], $_GET['school']);
or
$string = 'Hello, my name is :name and I go to :school';
echo str_replace(array(':name', ':school'), array($_GET['name'], $_GET['school']), $string);
You can automate that last one with something like:
function value_replace($values, $string) {
return str_replace(array_map(function ($v) { return ":$v"; }, array_keys($values)), $values, $string);
}
$string = 'Hello, my name is :name and I go to :school';
echo values_replace($_GET, $string);
No it won't work.
You have to define $name first before using it in another variable
config.php should look like
<?php
$name = htmlspecialchars($_GET['name']);
$school = htmlspecialchars($_GET['school']);
$mystring = "hello my name is $name and i got to $school";
and index.php like
<?php
include('config.php');
echo $mystring;
Why didn't you try it?
demo:
http://sandbox.phpcode.eu/g/2d9e0.php?name=martin&school=fr.kupky
Alternatively, you can use sprintf like this:
$mystring = "hello my name is %s and i got to %s";
// ...
printf($mystring, $name, $school);
This works because your $mystring literal is using double quotes, if you'd used single quotes then it would not work.
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
I have names in the form of Lastname, Firstname. In my database I have a different field for both the first and last.
I would like to use PHP to read everything before the comma as the lastname and everything after the comma as the firstname. What is the best way to accomplish this?
list($Lastname,$Firstname) = explode(",",$Name);
<?php
$names = explode( "," , $allNames);
// $names[0] and names[1] are first and last names
?>
with the explode function.
<?php
list($firstname, $lastname) = explode(',','Lastname, Firstname',2);
echo $firstname.' '.$lastname;
?>
If you'll use list();
while( list($fname,$lname) = explode(", ", $db->fetch() ) ) {
echo $lname . " " . $fname . "<br />";
}
Without list() and assining an array;
$name = explode( ", ", $db->fetch()->nameField );
// may be you want to do something with that array
// do something
// echoing
foreach( $name as $fname=>$lname ) {
echo $lname . " " . $fname . "<br />"
}
As nobody has mentioned it yet, to expressly meet the question requirements, you'll need to use the third parameter to explode()
list($lastname, $firstname) = explode(',', $name, 2);
Basically what I want to do is display an email using javascript to bring the parts together and form a complete email address that cannot be visible by email harvesters.
I would like to take an email address eg info#thiscompany.com and break it to:
$variable1 = "info";
$variable2 = "thiscompany.com";
All this done in PHP.
Regards,
JB
list($variable1, $variable2) = explode('#','info#thiscompany.com');
$parts = explode("#", $email_address);
Assuming that $email_address = 'info#thiscompany.com' then $parts[0] == 'info' and $parts[1] == 'thiscompany.com'
You can use explode:
$email = 'info#thiscompany.com';
$arr = explode('#',$email);
$part1 = $arr[0]; // info
$part2 = $arr[1]; // thiscompany.com
$email = "info#thiscompany.com";
$parts = explode("#", $email);
Try this one before you roll your own (it does a lot more):
function hide_email($email)
{ $character_set = '+-.0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set); $cipher_text = ''; $id = 'e'.rand(1,999999999);
for ($i=0;$i<strlen($email);$i+=1) $cipher_text.= $key[strpos($character_set,$email[$i])];
$script = 'var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";';
$script.= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
$script.= 'document.getElementById("'.$id.'").innerHTML=""+d+""';
$script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")";
$script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>';
return '<span id="'.$id.'">[javascript protected email address]</span>'.$script;
}
How about a function for parsing strings according to a given format: sscanf. For example:
sscanf('info#thiscompany.com', '%[^#]#%s', $variable1, $variable2);
Here's some PHP code:
$myText = 'ABC #12345 (2009) XYZ';
$myNum1 = null;
$myNum2 = null;
How do I add the first set of numbers from $myText after the # in to $myNum1 and the second numbers from $myText that are in between the () in to $myNum2. How would I do that?
preg_match('/#(\d+).*\((\d+)\)/', $myText, $matches);
$myNum1 = $matches[1];
$myNum2 = $matches[2];
assuming you have something like:
" stuff ... #123123 stuff (456456)"
that will give you
$myNum1 = 123123
$myNum2 = 456456
If you have an input string of form "123#456", you can do
$tempArray = explode("#", $input);
if (sizeof($tempArray) != 2) {
echo "OH NO! Something bad happened!";
}
$value1 = intval($tempArray[0]);
$value2 = intval($tempArray[1]);
echo "Result: " . ($value1 + $value2);