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 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);
Related
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 2 years ago.
Improve this question
I want split one group AS follow
splitGroups("11133355557777") ➞ ["111", "333", "5555", "7777"]
Anyone have idea then let me know
With php version 5 above this will work link to execute
Link to an example https://paiza.io/projects/qfRZ07OP3OviWCVsUbdFtQ
function splitGroups($str){
$arr = [];
$i=0;
$sub = '';
while($i!=strlen($str))
{
$sub .= $str[$i];
if ( strlen($str)-1 == $i || $str[$i] != $str[$i+1] ){
$arr[] = $sub;
$sub='';
}
$i++;
}
return $arr;
}
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 want to use a loop to iterate through my array, calling my function to print out all of these messages. I have to somehow keep track - I'm using PHP code.
This my code :
<? php
$count = 6;
$rp = 11000;
$amount = array(1000,1000,1500,500,2000,4000);
foreach ($amount as $v) {
echo $total = $rp-$v; ?>
my output should be:
10000
9000
7500
7000
5000
1000
Is this what you want?
<?php
$count = 6;
$rp = 11000;
$amount = array(1000,1000,1500,500,2000,4000);
foreach ($amount as $v) {
$rp = $rp - $v;
$count--;
//echo $count;
echo $rp."<br/>" ;
}
?>
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 6 years ago.
Improve this question
I am trying to create an associative array that will parse an alphabet for each month name. e.g JAN - A, FEB - B, MAR - C... I need some help for this. I have done a lot of searching but not able to. Can anyone help please?
Thanks in advance.
<?php
$my_month = date('m');
$months = array("JAN"=>"A",
"FEB"=>"B",
"MAR"=>"C",
"APR"=>"D",
"MAY"=>"E",
"JUN"=>"F",
"JUL"=>"G",
"AUG"=>"H",
"SEP"=>"I",
"OCT"=>"J",
"NOV"=>"K",
"DEC"=>"L" );
if ($my_month == date('M')) {
echo $months[$my_month];
} else {
echo 'almost there';
}
?>
The code you need to create an array as you want is this:
$months['1'] = 'A';
$months['2'] = 'B';
$months['3'] = 'C';
$months['4'] = 'D';
$months['5'] = 'E';
$months['6'] = 'F';
$months['7'] = 'G';
$months['8'] = 'H';
$months['9'] = 'I';
$months['10'] = 'J';
$months['11'] = 'K';
$months['12'] = 'L';
Or even better:
$months = array("1"=>"A",
"2"=>"B",
"3"=>"C",
"4"=>"D",
"5"=>"E",
"6"=>"F",
"7"=>"G",
"8"=>"H",
"9"=>"I",
"10"=>"J",
"11"=>"K",
"12"=>"L" );
And let's say the $my_month variable has the month (in the form 'JAN', 'FEB', ...) from which you want to get the corresponding letter. This is what you write to get the letter:
echo $months[$my_month];
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 am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)
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
How is it possible to choose a specific word/s or number/s from a link with php. I have the following urls where I want to choose only the rss number. Have been trying to use preg_match and preg_replace to no avail unfortunately.
<link><![CDATA[http://www.domain.com/league/news/newsid=21898248704.html?rss=2148704+hakska+iwumao+oioqp+badge+water]]></link>
<link><![CDATA[http://www.domain.com/rugby/video/ball/index.html?rss=2133483+water+none+respective+all+sat's+report]]></link>
As you can see the urls are not the same but both have rss=XXXXXX. My aim is to insert the number after "rss=" into the database.
Would appreciate if anyone can give me a tip of how to do this.
Like this ?
<?php
$link = '<link><![CDATA[http://www.domain.com/league/news/newsid=21898248704.html?rss=2148704+hakska+iwumao+oioqp+badge+water]]></link>';
preg_match('/rss=([0-9]+)/', $link, $matches);
echo $matches[1]; // returns 2148704
?>
$url = "http://www.example.com/foo.php?rss=9001+asdf";
$res = "";
$i = 0;
if (($i = strpos($url, 'rss=')) !== false) {
$res = substr($url, $i + 4); // +4 because the length of 'rss=' is 4, we just want the value
if (($i = strpos($res, '+')) !== false) { // $res contains +, cut that off
$res = substr($res, 0, $i);
}
// res has a value
echo $res;
} else {
// res has no value
}
Example
$link = '';
preg_match('/rss=([0-9]+)/', $link, $matches);
echo $matches[1]; // returns 2148704
Worked fine. Thanks a lot Phantom!