опитвам се да направя един readmore с този javascript http://jedfoster.com/Readmore.js/, с текстове си работи, но незнам как да го направя за php някакви идеи ?
Обясни по-подробно каква е идеята.
Е прави height на div-a примерно 50 пиксела, което ще показва 400-500 знака. Като се натисне бутон "read more" да ти прави height-a на auto и ще си готов. Чист javascript без плугини, че даже можеш и само с css да го сториш.
PHP изпълнява от сървъра, JS се изпълнява от браузара.
Да не говориш за AJAX?! Или пък от страница А с резюме на текста и бутон readmore да се ходи на страница Б с целия текст?
Ето ти пример с чисто jQuery без въпросния плъгин: https://jsfiddle.net/jLLbcwho/
function get_words($sentence, $count = 10) {
preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}
Обаче всички тия калкулации на сървъра, може да му дойде множко при големи статии и много посетители. По принцип като се пише статия още при съставянето се отделя краткия текст от целия, но ако все пак е възможен само целия аз бих търсил решение с JavaScript
JS мие малко зор, но няма смисъл да се намесва PHP тук.Човек, ще вземе да ти повярва някои и ще почне да пише някакви измислени скриптове![]()
function get_words($string, $count = 10) {
$retval = [];
$pieces = explode(" ", $string);
$retval[0] = implode(" ", array_splice($pieces, 0, $count));
$retval[1] = implode(" ", array_splice($pieces, $count));
return $retval;
}
function get_words2($string, $count = 10) {
$words = explode(" ", $string);
$first = join(" ", array_slice($words, 0, $count));
$rest = join(" ", array_slice($words, $count));
return [$first, $rest];
}
$txt="";
for($i=0; $i<1000; $i++)
{
$txt.=" дума";
}
$t = microtime(true);
for($i=0; $i<1000; $i++)
{
get_words($txt, 200);
}
echo (microtime(true)-$t)."<br>";
$t = microtime(true);
for($i=0; $i<1000; $i++)
{
get_words2($txt, 200);
}
echo (microtime(true)-$t)."<br>";
get_words1 - total time: 1.135065, iterations: 100000, average time: 0.000011
get_words2 - total time: 17.017973, iterations: 100000, average time: 0.000170
<?php
function get_words1($sentence, $count = 10) {
preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}
function get_words2($string, $count = 10) {
$words = explode(" ", $string);
$first = join(" ", array_slice($words, 0, $count));
$rest = join(" ", array_slice($words, $count));
return [$first, $rest];
}
$file = file_get_contents('lorem.txt');
$time1start = microtime(true);
for ($i=0; $i<100000; $i++)
{
$words = get_words1($file);
}
$time1end = microtime(true);
$msg = sprintf("get_words1 - total time: %f, iterations: %d, average time: %f",
$time1end - $time1start, 100000, ($time1end - $time1start) / 100000);
echo $msg . '<br/>';
$time2start = microtime(true);
for ($i=0; $i<100000; $i++)
{
$words = get_words2($file);
}
$time2end = microtime(true);
$msg = sprintf("get_words2 - total time: %f, iterations: %d, average time: %f",
$time2end - $time2start, 100000, ($time2end - $time2start) / 100000);
echo $msg . '<br/>';
C:\>ab -n 100 -c 10 http://localhost/testwords1.php
This is ApacheBench, Version 2.3 <$Revision: 1554214 $>
Benchmarking localhost (be patient).....done
Server Software: Apache/2.4.9
Server Hostname: localhost
Server Port: 80
Document Path: /testwords1.php
Document Length: 82 bytes
Concurrency Level: 10
Time taken for tests: 78.107 seconds
Complete requests: 100
Failed requests: 9
(Connect: 0, Receive: 0, Length: 9, Exceptions: 0)
Total transferred: 26909 bytes
HTML transferred: 8209 bytes
Requests per second: 1.28 [#/sec] (mean)
Time per request: 7810.747 [ms] (mean)
Time per request: 781.075 [ms] (mean, across all concurrent requests)
Transfer rate: 0.34 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.4 1 1
Processing: 6952 7776 1079.6 7290 11309
Waiting: 6952 7776 1079.5 7290 11309
Total: 6953 7777 1079.6 7290 11310
Percentage of the requests served within a certain time (ms)
50% 7290
66% 7380
75% 8247
80% 8279
90% 9782
95% 10219
98% 11032
99% 11310
100% 11310 (longest request)
C:\>ab -n 100 -c 10 http://localhost/testwords2.php
This is ApacheBench, Version 2.3 <$Revision: 1554214 $>
Benchmarking localhost (be patient)...apr_pollset_poll: The timeout specified ha
s expired (70007)
function trimText($string, $character){
$text = $string;
$trim = substr($string,0,$character);
$trim = substr($trim,0,strrpos($trim," "));
$rest = substr($text, strlen($trim));
return array($trim, $rest);
}
$txt="";
for($i=0; $i<1000; $i++)
{
$txt.=" дума".rand();
}
$t = microtime(true);
for($i=0; $i<1000; $i++)
{
trimText($txt, 200);
}
echo ((microtime(true)-$t)*1000)."<br>";
preg_match("/(?:\w+(?:\W+|$)){0,2}/", "бла бла бла бла бла бла", $matches);
print_r($matches);