Please specify how to implement this code between echo ' '; [closed] - php

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
I have using this code between echo "";
<?php
for ($i=0; $i<$tot; $i++)
{
$rt = date ( "d-m-Y", $y[$i] ) ;
}
?>
Please specify how to implement this code between echo "";

<?php
$output = 'for ($i=0; $i<$tot; $i++){ ';
$output .= '$rt = date ( \'d-m-Y\', $y[$i] );';
$output .= '}';
echo $output;
?>
For readability I separated it into 4 lines, but you can see how it could easily be refactored into one echo statement.
The key idea here is that by declaring strings with ' instead of ", php will not interpret the $ signs as variables. Additionally, in order to include single quotes within the string itself, a backslash escape character is required as in \'d-m-Y\'

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];
}

Create 10-9-8-7-6-5-4-3-2-1 in for loop php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Create a script that displays 10-9-8-7-6-5-4-3-2-1. there will be no hyphen(-) at starting and ending position in php for loop.
I have created this
<?php
for($i=1; $i<=10; $i++)
{
if($i!=10)
{
echo "$i-";
}
else
{
echo "$i";
}
}
?>
outputs
1-2-3-4-5-6-7-8-9-10
but I want this result 10-9-8-7-6-5-4-3-2-1
One line, using range to generate the values from 10 down to 1, and implode to put them all together with hyphens between them:
echo implode('-', range(10, 1));
Output:
10-9-8-7-6-5-4-3-2-1
Demo on 3v4l.org
<?php for($i=10; $i>=1; $i--){
if($i!=1) {
echo "$i-";
} else {
echo "$i";
}
}
?>

How to divide CSV to two different Strings in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to divide CSV into Two String LHS_String and RHS_String. For Example, I have the below CSV -
customer^id,record^id
customer^fname,record^first_name
cusomer^gender,record^gender
The Output should be like -
LHS_String : customer^id
RHS_String : record^id
Short answer, read over http://php.net/manual/en/function.explode.php
Example:
<?php
$data = "customer^id,record^id\r\n";
$data .= "customer^fname,record^first_name\r\n";
$data .= "cusomer^gender,record^gender\r\n";
$p1 = explode("\r\n", $data);
$p2 = array();
foreach($p1 as $key => $line){
$p2[$key] = explode(",", $line);
}
echo "LHS_String : " . $p2[0][0] . "\r\n";
echo "RHS_String : " . $p2[0][1] . "\r\n";
?>

Resolve PHP post string [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 7 years ago.
Improve this question
I have the following post string:
Name=John&Age=33&Question1=What's+your+gender%3F&Answer1=Male&Question2=What's
+your+education%3F&Answer2=Graduate
I'm going to have randomly generated amount of questions each time so I won't be able to know number of passed variables. Thus, I am searching for a solution something like:
foreach($postItem as $varr) {
echo $varr["name"].": ".$varr["value"]."<br />";
}
Post them as answer[1], answer[2] etc instead of answer1, answer2, and PHP will turn them into an array for you.
//parse query string to an array ($output)
parse_str($postString, $output);
echo $output['Name'] . ': ' . $output['Age'] . '<br />';
//process questions
$i = 1;
while (array_key_exists('Question' . $i, $output)) {
echo $output['Question' . $i] . ': ' . $output['Answer' . $i] . '<br />';
$i++;
}
function used http://php.net/parse_str

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