Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: Помогите изменить формат вывода случайной строки
Форумы портала PHP.SU » » Вопросы новичков » Помогите изменить формат вывода случайной строки

Страниц (1): [1]
 

1. IgorGr - 01 Ноября, 2019 - 17:51:14 - перейти к сообщению
Здравствуйте Други!
Прошу помочь в решении проблемы.
Знания поверхностные и поэтому использовал всю свою скудную "библиотеку" так и не смог найти решение.
Имеется функция вывода случайной строки из файла:
PHP:
скопировать код в буфер обмена
  1. function RandLine($num,$s,$fileName){
  2.                 $data = file($fileName);
  3.                 $arr = array();
  4.                 $output = "";
  5.                
  6.  
  7.                 for ( $i = 0; $i < $num; $i++ ) {
  8.                        
  9.                         do {
  10.                                 $rand = mt_rand(0, count($data) - 1);
  11.                         } while ( in_array($rand, $arr) );
  12.                        
  13.                         $arr[] = $rand;
  14.                         $output .= str_replace("\n", "", $data[$rand]).$s;
  15.                 }
  16.                 return $output;
  17. }
  18. ****************Изменить этот кусок кода********************
  19. function GenTheText( $t ) {
  20.         while ( preg_match( '#\{([^\{\}]+)\}#i', $t, $m ) ) {
  21.                 $v = explode( '|', $m[1] );
  22.                 $i = rand( 0, count( $v ) - 1 );
  23.                 $t = preg_replace( '#'.preg_quote($m[0]).'#i', $v[$i], $t, 1 );
  24.         } return $t;
  25. }
  26. $str='';
  27. ***********************************************************
  28. function mb_ucfirst($str) {
  29.      return mb_substr(mb_strtoupper($str,'utf-8'),0,1,'utf-8').mb_strtolower(mb_substr($str,1,mb_strlen($str,'utf-8'),'utf-8'),'utf-8');
  30. }
  31.  
  32.  
  33. $public = RandLine($num = 1,$s = "", $dir."public.txt");
  34. $attach = "img/".$public.".txt";
  35. $rand36 = rand(1,1);
  36. $attachments = RandLine($num = $rand36,$s = ", ", $attach);
  37. $randtext = file_get_contents($dir."text/".$public.".txt");
  38.  
  39. $text = GenTheText($randtext);
  40.  
  41. echo $public."<br/>".$attach."<br/>".$attachments."<br/>".$text;
  42. $ch = curl_init('https://api.vk.com/method/wall.post');
  43. curl_setopt ($ch, CURLOPT_HEADER, 1);
  44. curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
  45. curl_setopt ($ch, CURLOPT_REFERER, 'https://api.vk.com/method/wall.post');
  46. curl_setopt ($ch, CURLOPT_POST, 1);
  47. curl_setopt ($ch, CURLOPT_COOKIEJAR, $dir.'cookie.txt');
  48. curl_setopt ($ch, CURLOPT_POSTFIELDS, 'owner_id=-'.$public.'&v=5.28&access_token='.$access_token.'&attachments='.$attachments.'&message='.$text);
  49. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  50. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  51. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  52. curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  53. curl_exec ($ch);
  54. $result = curl_multi_getcontent ($ch);
  55. echo "\n".'Login OK'."\n".'[result ===8<===>'."\n".$result."\n".'<===>8=== result]'."<br/>\n";
  56. echo "http://vk.com/public".$public."";
  57. curl_close ($ch);
  58.  
  59. ?>

Но эта функция имеет недостаток по ограничению количества символов в 30 тыс.
Пример:
{привет|здравствуй|как дела|.....}-30 тыс.
При добавлении вариантов слов или словосочетаний приходит ответ:
Compilation failed: regular expression is too large at offset 291712 in C:\OpenServer\OSPanel\domains\ut316\send.php on line 31
Если возможность уйти от формата {|} и перейти на формат:
текст1
текст2
текст3
.....
Необходимо брать случайную текстовую строку из файла объемом хотя бы 1 мб
Текствый файл находиться в папке text
2. Строитель - 01 Ноября, 2019 - 23:00:06 - перейти к сообщению
IgorGr, можно попробовать разбивать строку "привет|здравствуй|как дела|....." на подстроки, и эти отдельные слова подставлять в шаблон в цикле. Т.е., как-то так:
Спойлер (Отобразить)
3. IgorGr - 02 Ноября, 2019 - 16:02:35 - перейти к сообщению
Спасибо большое за помощь. Вопрос решен

 

Powered by ExBB FM 1.0 RC1