Mathematical operation of CHARACTER [not numeric] in php [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
is there any way to sum/substrate character in php?
For example if
$var1 = 'a';
$var2 = 'b';
$var3 = 'a';
$calculation = $var1 - $var2 + $var3;
echo $calculation;
I want the output as 2a-b
Just like we did in high school algebra?

I wrote a simple function to make something like what you want.
It's just an example, you will have to improve it a lot if you really want to use it, but is a good start.
Limitations:
Only works with letters (Won't work propely if you add numbers, you will have to add that functionaliy).
ALL the letters must have their plus or minus.
You must use spaces before a plus or minus.
This is definitely not the best way to do it, as I said you have to improve it. I wrote it fast but I tested it a bit.
<?
function calc($str){
$data = preg_split("/ /", $str);
$used = Array();
$buffer = "";
foreach ($data as $pos=>$letter){
foreach ($data as $pos2=>$letter2){
if ($letter[1] == $letter2[1] && !in_array($pos, $used) && !in_array($pos2, $used) && $pos != $pos2){
$first = $letter[0] == '+' ? 1 : -1;
$second = $letter2[0] == '+' ? 1 : -1;
$buffer .= ($first+$second).$letter[1];
$used[count($used)] = $pos;
$used[count($used)] = $pos2;
}
}
}
foreach ($data as $pos=>$letter){
if (!in_array($pos, $used)){
$buffer .= $letter;
}
}
return $buffer;
}
echo calc("+a -b +a");
?>
Output:
2a-b

Related

Obfuscate email address on php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to obfuscate or cover an email address on PHP.
For this, i have the next code.
<td>
<p class="list-item-heading"><small><?= $row->email ?></small></p>
</td>
Where $row->email is
realname_x123#gmail.com
.
I need to show this as
r*al****_x*2*#gmail.com
I need to replace with * ALWAYS the same parts of the string, not randomly because it will be shown on a list, maximun of 5 or 6 characters visible.
Anytips? I've tried with strpos, and str_replace with no success.
EDIT:
IF this cannot be do. It will be usefull also, for example, to only leave 3 chars from the beggining.
rea***********#gmail.com
I've found a workaround that suits for me.
<?php
function hideEmail($email)
{
$mail_segments = explode("#", $email);
$mail_segments[0] = substr($mail_segments[0], 0, 1) . str_repeat("*", strlen($mail_segments[0]) - 2) . substr($mail_segments[0], -1);
$pos = strpos($mail_segments[1], '.');
$mail_segments[1] = substr($mail_segments[1], 0, 1) . str_repeat("*", strlen($mail_segments[1]) - $pos+1) . substr($mail_segments[1], $pos-1);
return implode("#", $mail_segments);
}
?>
function mailObfuscate($mail) {
//every second character replace with *
$mail = explode('#',$mail);
$array = str_split($mail[0]);
$control = 0;
$ret = '';
foreach ($array as $char) {
if ($control == 0) {$ret .= '*'; $control++;} else {$ret .= $char; $control=0;}
}
return $ret.'#'.$mail[1];
}

Find first zero bit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What's the easiest way to find the first zero bit with PHP?
For example, say I have an integer 47 which is 111101, how can I find out that the 5th bit is the first unset bit? This needs to work to cater for different integers.
$value = 47;
$i = $j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
$i++;
}
echo "bit $i is 0";
If you want to eliminate the use of $i as a counter, you can do a little bit of extra math:
$value = 47;
$j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
}
echo "bit ", (log($j) / log(2) + 1), " is 0", PHP_EOL;
The +1 is necessary because you're starting your binary as bit 1 rather than as bit 0
Use decbin to return a string of 0 and 1.
Then, use strpos to find the first 0 caracters.
$str = decbin(47);
$result = strpos($str, '0');

Multiple statements in Ternary Operator [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to convert a small condition in php to ternary operator.
if($i==2) {
$third="third";
$i=0;
} else {
$third="";
}
How to Translate this to ternary operator?
Thanks.
You can use like this format:
$i==2 ? ($third = "third" AND $i = 0) : ($third="");
Because i'm bored:
list($i, $third) = array(($not2 = ($i != 2)) * $i, ($not2) ? '' : 'third');
or
list($i, $third) = ($i == 2) ? array(0, 'third') : array($i, '');
or maybe
$third = ($i == 2 ? "third" : '') and $i = 0;
or
$i *= !($third = ($i == 2) ? 'third' : '');
But don't do this in real life. Seriously. You have a perfectly readable if; there's no good reason to turn it into a mess like this.

transform the numbers to letters using php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I store the number 1 2 3 4 as the answer to multiple choice questions in MySQL database,
But I want to display them in a b c d on web page.
I'm using PHP, how can I transform the numbers to letters using PHP?
echo "
<h2>question</h2>
<p>".$row[0]."</p>
<h2>answer</h2>
<p>".$row[1]."</p>";
The row[1] is {1,2,3,4} now, I want it turn to {A,B,C,D}.
Can any one please have a look and tell me how to do it?
It is very easy:
<?php
$row = array(1, 2, 3, 4);
echo "
<h2>question</h2>
<p>".chr(64 + $row[0])."</p>
<h2>answer</h2>
<p>".chr(64 + $row[1])."</p>";
Test it: http://3v4l.org/uojsK
you can define an array first:
$letters = array_combine(range(1,26), range('a', 'z'));
//now just use:
echo $letters[$row[1]];
Create the alphabet array and use the numbers to get the right letter by index (num-1)
$letters = range('a','z');
$num = 3;
var_dump($letters[$num-1]);//"c"
i.e.
$letters = range('a','z');
echo "
<h2>question</h2>
<p>".$letters[$row[0]-1]."</p>
<h2>answer</h2>
<p>".$letters[$row[1]-1]."</p>";
<?php
/// the $row is the array you can get from db
$row = array(1,2,3,4,5);
// Here you can add your option up-to z
$alpha = range('A','E');
$i = 0;
/// Instead of foreach loop you can run while loop
foreach($row as $rows){
echo $alpha[($row[$i]-1)].'<br/>';
$i++;
}
?>

Looking for improvement on PHP split and combination function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose I have a string such as this:
ABC.DEF.GHI.JKL (This string could have any length and amount of characters between dots)
I want to add the following combinations into an array.
ABC
ABC.DEF
ABC.DEF.GHI
ABC.DEF.GHI.JKL
So basically the string should be split with the dot character and then the individual sub-strings should be combined.
I have a function already, but in my opinion this seems too complicated to achieve the result.
$myarray = array();
$split = explode('.', 'that.string.with.dots');
$string = '';
for ($i = 0; $i < count($split); $i++) {
$string .= $split[$i];
myarray[] = $string;
$string .= '.';
}
Any suggestions on improving this?
Thanks
Michael
Perhaps this:
$split = explode('.', 'that.string.with.dots');
for ($i = 1; $i < count($split); $i++) {
$split[i] = $split[$i-1] . '.' . $split[i];
}
It just concats the current with the previous.
I have the feeling that you are asking if there is a PHP function that will achieve this result with a single function call, but I don't think there is one.
You can rewrite the code as pritaeas did in his answer, to remove two lines of code, but it will still be the same concept.

Categories