Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z polskimi znakami
Forum PHP.pl > Forum > PHP
fajny_nick
Witam,

Mam problem z funkcją generującą BBCODE url z atrybutu title wstawianego linku. Są dwie następujące funkcje:


Pierwsza:
  1. function convert_url_to_bbcode_callback_auto_title($messagetext)
  2. {
  3. $messagetext = str_replace('\"', '"', $messagetext);
  4.  
  5. static $urlSearchArray, $urlReplaceArray, $emailSearchArray, $emailReplaceArray;
  6. if (empty($urlSearchArray))
  7. {
  8. $taglist = '\[b|\[i|\[u|\[left|\[center|\[right|\[indent|\[quote|\[highlight|\[\*' .
  9. '|\[/b|\[/i|\[/u|\[/left|\[/center|\[/right|\[/indent|\[/quote|\[/highlight';
  10. $urlSearchArray = array(
  11. "#(^|(?<=[^_a-z0-9-=\]\"'/@]|(?<=" . $taglist . ")\]))((https?|ftp|gopher|news|telnet)://|www\.)((\[(?!/)|[^\s[^$`\"'|{}<>])+)(?!\[/url|\[/img)(?=[,.!)]*(\)\s|\)$|[\s[]|$))#siU"
  12. );
  13.  
  14. $urlReplaceArray = array(
  15. );
  16.  
  17. $emailSearchArray = array(
  18. "/([ \n\r\t])([_a-z0-9-]+(\.[_a-z0-9-]+)*@[^\s]+(\.[a-z0-9-]+)*(\.[a-z]{2,4}))/si",
  19. "/^([_a-z0-9-]+(\.[_a-z0-9-]+)*@[^\s]+(\.[a-z0-9-]+)*(\.[a-z]{2,4}))/si"
  20. );
  21.  
  22. $emailReplaceArray = array(
  23. "\\1[email]\\2[/email]",
  24. "[email]\\0[/email]"
  25. );
  26. }
  27.  
  28. $text = preg_replace_callback($urlSearchArray, "auto_title", $messagetext);
  29.  
  30. if (strpos($text, "@"))
  31. {
  32. $text = preg_replace($emailSearchArray, $emailReplaceArray, $text);
  33. }
  34.  
  35. return $text;
  36. }



Druga:
  1. function auto_title ($text) {
  2.  
  3. $options = array( 'http' => array(
  4. 'user_agent' => 'worldwidecreations.com Linker',
  5. 'max_redirects' => 10,
  6. 'timeout' => 60,
  7. ) );
  8. $context = stream_context_create( $options );
  9.  
  10. if ($text[2] == 'www.' or $text[2] == 'WWW.') { $text[4] = $text[2] . $text[4]; $text[2] = 'http://'; }
  11.  
  12. $paged = @fopen( $text[2] . $text[4], "r" );
  13. if ($paged) {
  14. while (!feof($paged) and ($x < 1000)) { $page .= fread($paged, 8192); $x++; }
  15. fclose($paged);
  16. }
  17.  
  18. preg_match("/<title>[\n\r\s]*(.*)[\n\r\s]*<\/title>/", $page, $title);
  19.  
  20. if ($title[1] == '') { $title[1] = $text[2] . $text[4]; }
  21. return "" . preg_replace("/\&.+\;/", '', $title[1]) . "";
  22. }



No i na koniec wywołanie (wstawiony link to żadna reklama, tylko przykładowa strona z google, która ma w title polskie znaki):
  1. $messagetext='http://histmag.org/forum/index.php?topic=9722.0';
  2.  
  3. $messagetext = preg_replace(
  4. '#(^|\[/(' . $skiptaglist . ')\])(.*(\[(' . $skiptaglist . ')|$))#siUe',
  5. "convert_url_to_bbcode_callback_auto_title('\\3', '\\1')",
  6. $messagetext
  7. );
  8.  
  9. echo convert_url_to_bbcode_callback_auto_title($messagetext);


No i na koniec dostaje takie krzaki:
Kod
[url="http://histmag.org/forum/index.php?topic=9722.0"]Zażółć gęślą jaźń - przytulisko młodego kopirajtera.[/url]


Podejrzewam, że błąd jest w pierwszej funkcji w tym miejscu:
  1. $urlSearchArray = array(
  2. "#(^|(?<=[^_a-z0-9-=\]\"'/@]|(?<=" . $taglist . ")\]))((https?|ftp|gopher|news|telnet)://|www\.)((\[(?!/)|[^\s[^$`\"'|{}<>])+)(?!\[/url|\[/img)(?=[,.!)]*(\)\s|\)$|[\s[]|$))#siU"
  3. );


Ale sam sobie nie poradzę z tym problemem, mogę prosić o pomoc?
maniana
Kodowanie na pewno dobre?
fajny_nick
Niestety ale tak. Już mi po prostu pomysłów brak.

Pozwolę sobię odświeżyć.
Sinevar
Spróbuj z tym. U mnie działa smile.gif

  1. header('Content-Type: text/html; charset=utf-8');
  2. echo convert_url_to_bbcode_callback_auto_title($messagetext);
muniekw
Sprawdź kodowanie plików bo domyślam się, że Twoje kodowanie plików jest ustawione na ANSI i stąd Ci krzaczy.

Zmień sobie kodowanie pliku na UTF-8. Możesz to zrobić np Notepad++ (zakładka Format)
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.