php include images randomly, echo and seperate by commas? [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 want to include files, and if exist echo the filename with extension like 1.jpg, but it need to be randomly and seperated by commas like these
('1.jpg','2.jpg','3.jpg','4.jpg')
, but if some of pictures dont exist in specified follder, need to echo manually, example if there are only three images on folder, need to be like this
( 1.jpg,'2.jpg','3.jpg','default.jpg')
, Hope someone understand what im asking for.

Try this:
<?php
$images = glob('/images/*.{jpeg,gif,png}', GLOB_BRACE);
shuffle($images);
$out = '(';
foreach($images as $img)
{
$out .= '"';
$out .= file_exists($img) ? $img : 'default.jpg';
$out .= '",';
}
$out .= ')';
echo $out;
?>

<?php
// set minimum number of images to show
$show_min=4;
// define manual images
$manual[]="1.jpg";
$manual[]="2.jpg";
$manual[]="3.jpg";
$manual[]="4.jpg";
// shuffle images
$images_found = glob("images/bg/*.{jpg,png,gif}", GLOB_BRACE)
shuffle($images_found);
$result = '(';
foreach($images_found as $image)
{
$result.='"' . $image . '",';
}
if (count(images_found) < $show_min)
{
for($i=0;$i<($show_min-count(images_found));$i++)
$result.='"' . $manual[$i] . '",';
}
$result .= ')';
//fix for last comma
$result=str_replace(",)", ")", $result);
echo $result;
?>
Hope it works.

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

Given a string that is a range of excel cells 'A1:F1' how would one go about interpolating this range in 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 3 years ago.
Improve this question
I would like to fill this 'A1:F1' out to either an array or a string such that it now equals array {A1, B1, C1, D1, E1, F1} or "A1:B1:C1:D1:E1:F1"
Does anyone have an efficient idea on how to do this?
Thanks.
This is one possible solution. It works in two dimensions. As can be seen in the output, it works column by row, to reverse that simply requires changing the order of the two for loops.
function split_range($range) {
list($start_cell, $end_cell) = explode(':', $range);
if (!preg_match('/^([A-Z]+)(\d+)$/', $start_cell, $start) || !preg_match('/^([A-Z]+)(\d+)$/', $end_cell, $end)) return array();
$cells = array();
for ($c = $start[1]; str_pad($c, strlen($end[1]), " ", STR_PAD_LEFT) <= $end[1]; $c++) {
for ($r = $start[2]; $r <= $end[2]; $r++) {
$cells[] = "$c$r";
}
}
return $cells;
}
echo implode(':', split_range("A1:F1")) . "\n";
echo implode(':', split_range("A1:A6")) . "\n";
echo implode(':', split_range("B2:D4")) . "\n";
echo implode(':', split_range("Q3:AB4")) . "\n";
Output:
A1:B1:C1:D1:E1:F1
A1:A2:A3:A4:A5:A6
B2:B3:B4:C2:C3:C4:D2:D3:D4
Q3:Q4:R3:R4:S3:S4:T3:T4:U3:U4:V3:V4:W3:W4:X3:X4:Y3:Y4:Z3:Z4:AA3:AA4:AB3:AB4
If I don't misunderstood your question, then is one solution to achieve what you said on your question using php. But let me know if I am on wrong track :)
<?php
$str = 'A1:F1';
$arr = explode(":",$str);
$start = $arr[0];
$end = $arr[1];
$range = range($start[0],$end[0]);
$func = function($value) use ($start){
return $value.$start[1];
};
$result = array_map($func,$range);
echo "{".implode(',',$result)."}";
echo PHP_EOL;
echo implode(':',$result);
?>
Output:
{A1,B1,C1,D1,E1,F1}
A1:B1:C1:D1:E1:F1
DEMO: https://3v4l.org/iDsGe

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

Please specify how to implement this code between echo ' '; [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
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\'

Categories