Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Dzielenie tekstu na dwie równe części
arzach
post
Post #1





Grupa: Zarejestrowani
Postów: 332
Pomógł: 6
Dołączył: 27.11.2008

Ostrzeżenie: (0%)
-----


Witam mam próbuje podzielić tekst na dwie równe części i między ten tekst wstawić reklamę

Zrobiłem ale to nie jest najlepsze rozwiązanie ponieważ dzieli źle wyrazy np. jak mam test to wychodzi t est itp. czy można to zrobić lepiej ?

  1. $news = "TEST TEST TEST TESTTTTTTT TES TSTST";
  2.  
  3. $ceil = ceil(strlen($news) / 2);
  4.  
  5. $news = str_split($news, $ceil);
  6.  
  7.  
  8. $reklama = "<script type=\"text/javascript\"><!--
  9. google_ad_client = \"pub-3995387288904286\";
  10. /* 160x90, utworzono 10-09-24 */
  11. google_ad_slot = \"1776115406\";
  12. google_ad_width = 160;
  13. google_ad_height = 90;
  14. //-->
  15. </script>
  16. <script type=\"text/javascript\"
  17. src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
  18. </script>";
  19.  
  20.  
  21.  
  22. $news = $news[0]."<div style='text-align:center;'></br>".$reklama."<div></br>".$news[1];


Ten post edytował arzach 5.10.2010, 19:17:46
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
qrzysztof
post
Post #2





Grupa: Zarejestrowani
Postów: 220
Pomógł: 19
Dołączył: 25.04.2009

Ostrzeżenie: (0%)
-----


Mało założeń żeby wyrokować co jest potrzebne.

Ale można zrobić np tak:

  1. $exploded=explode(' ', $news);
  2.  
  3. $countAll=count($exploded);
  4. $countHalf=ceil($countAll/2);
  5.  
  6. for ( $i=0; $i <$countAll ; $i++ )
  7. {
  8. if ( $i<$countHalf )
  9. {
  10. $firstPart.=$exploded[$i].' ';
  11. }
  12. else
  13. {
  14. $secondPart.=$exploded[$i].' ';
  15. }
  16. }


Zamiast funkcji explode() można użyć np preg_split(), która wyłapie i usunie wielokrotne spacje.

Dużo zależy od długości tekstu. Zakładam, że będzie długi.

Ten post edytował qrzysztof 5.10.2010, 19:48:15


--------------------
Znalazłeś sam rozwiązanie swojego problemu? Nie pisz "już wiem, do zamknięcia". Podziel się rozwiązaniem - inni będą mieli łatwiej.
Go to the top of the page
+Quote Post
arzach
post
Post #3





Grupa: Zarejestrowani
Postów: 332
Pomógł: 6
Dołączył: 27.11.2008

Ostrzeżenie: (0%)
-----


Potrzebuje podzielnic tekst na mniej więcej dwie równe części ale tak by nie rozdzielało mi wyrazów

Próbowałem z
  1. $news = "TEST TEST ESSSSSSSSSeeeeeeeeeeeeeeeeeee TEST TEST TEESSSSSSSSSSSSSSS";
  2.  
  3. $ilosc = strlen($news);
  4. $polowa = ceil($ilosc/2);
  5. $news = strpos($news,"r\n", $polowa);
  6. echo $news

Ale to nie działa też

Ten post edytował arzach 5.10.2010, 19:46:07
Go to the top of the page
+Quote Post
Wicepsik
post
Post #4





Grupa: Zarejestrowani
Postów: 1 575
Pomógł: 299
Dołączył: 26.03.2009

Ostrzeżenie: (20%)
X----


  1. $news = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam eu lorem nisl, nec pulvinar enim. Nulla lectus libero, consectetur at viverra pellentesque, ullamcorper in urna. Curabitur dolor leo, commodo in aliquet ut, aliquet in tellus. Donec commodo odio at eros venenatis pellentesque. Aenean id tellus tellus. Nam vehicula, eros tristique tristique laoreet, dui felis condimentum tellus, in vestibulum ante felis ut metus. Ut arcu purus, volutpat vel ornare ut, tincidunt eget nisl. Suspendisse et diam sed arcu tempus varius. Nullam nec justo et felis euismod consectetur vel non nisi. Integer nulla metus, ullamcorper et viverra at, feugiat eget erat. Nulla. ";
  2.  
  3. $ilosc = strlen($news);
  4. $polowa = ceil($ilosc/2);
  5. $news = wordwrap($news, $polowa, "<br />");
  6. echo $news;


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 03:52