Cibiņš Posted October 5, 2012 Report Share Posted October 5, 2012 (edited) Nav apskaidrības, kā izvairīties no nezināmu simbolu iestarpināšanas apcērpjot tekstu. $name=$row['tschalipashki']; $character_limit=50; $replaced_header = array(' ', '&', 'Ā', 'ā', 'Č', 'č', 'Ē', 'ē', 'Ģ', 'ģ', 'Ī', 'ī', 'Ķ', 'ķ', 'Ļ', 'ļ', 'Ņ', 'ņ', 'Š', 'š', 'Ū', 'ū', 'Ž', 'ž'); $wrapped_header = array(' ', '&', 'Ā', 'ā', 'Č', 'č', 'Ē', 'ē', 'Ģ', 'ģ', 'Ī', 'ī', 'Ķ','ķ','Ļ','ļ','Ņ','ņ', 'Š', 'š', 'Ū', 'ū', 'Ž', 'ž'); $name = str_replace($wrapped_header, $replaced_header, $name); $check_text_lenght = strlen($name); if($check_text_lenght>=$character_limit){ $text_points='...'; } else { $text_points=''; } $truncated_header = substr($name, 0, $character_limit).$text_points; Ja "apcirpšana" iekrīt tieši tur, kur atrodas kāda garumzīme tā tas tiek parādīts kā jautājumsimbols. Kā lai izvairās no tādas situācijas? :( Piem ieliekam tekstu "Jānīc novēl saviem klientiem priecīgus Ziemassvētkus un laimīgu jauno gadu".. :( :( :( Tas beidzas "letāli".. :( Charactersets tabulai ir latin1 un collation-latin1_swedish_ci Edited October 5, 2012 by Cibiņš Quote Link to comment Share on other sites More sharing options...
zarins Posted October 5, 2012 Report Share Posted October 5, 2012 (edited) Izmanto mb_substr, nevis vienkārši substr. Tur arī vari noradīt kodējumu (utf-8) un nebūs problēmas ar neparadzētiem simboliem. Edited October 5, 2012 by zarins Quote Link to comment Share on other sites More sharing options...
Cibiņš Posted October 5, 2012 Author Report Share Posted October 5, 2012 Nah vispār vajag starta un gala punktus (vai kā viņus tur sauc)??? Tipa int $start [, int $length] īsti nedalec kā rindiņu uzskricelēt. Quote Link to comment Share on other sites More sharing options...
malinens Posted October 5, 2012 Report Share Posted October 5, 2012 (edited) $check_text_lenght = strlen($name); tas jādara pirms str_replace (to vispār pašās beigās jāliek) un ar mb_strlen: $check_text_lenght = mb_strlen($name); un visur jāizmanto fjas ar mb_ prefiksu, ja tādas ir. Piemēram: $truncated_header = substr($name, 0, $character_limit).$text_points; vietā vajag: $truncated_header = mb_substr($name, 0, $character_limit).$text_points; Ko tu darīsi, ja teksts būs kirilicā? Edited October 5, 2012 by malinens Quote Link to comment Share on other sites More sharing options...
Cibiņš Posted October 6, 2012 Author Report Share Posted October 6, 2012 (edited) Vispār nemainās, taspats gļuks vien ir, kad nowrapo tekstu kur teksta garums beidzas ar garumzīmi to garumzīmi atspoguļo kā jautājumzīmi iekš rombiņa :( Pietam vispār man liekas ka tad jau var iztikt bez tā replace jo teorētiski tas bija domāts aizvietot nobrukušos teksta simbolus.. $name='ģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīšģlāžšķūņīš'; $character_limit=20; $check_text_lenght = mb_strlen($name); if($check_text_lenght>=$character_limit){ $text_points='...'; } else { $text_points=''; } $truncated_header = mb_substr($name, 0, $character_limit).$text_points; $replaced_header = array(' ', '&', 'Ā', 'ā', 'Č', 'č', 'Ē', 'ē', 'Ģ', 'ģ', 'Ī', 'ī', 'Ķ', 'ķ', 'Ļ', 'ļ', 'Ņ', 'ņ', 'Š', 'š', 'Ū', 'ū', 'Ž', 'ž'); $wrapped_header = array(' ', '&', 'Ā', 'ā', 'Č', 'č', 'Ē', 'ē', 'Ģ', 'ģ', 'Ī', 'ī', 'Ķ','ķ','Ļ','ļ','Ņ','ņ', 'Š', 'š', 'Ū', 'ū', 'Ž', 'ž'); $name_str = str_replace($wrapped_header, $replaced_header, $truncated_header); echo $name_str; Edited October 6, 2012 by Cibiņš Quote Link to comment Share on other sites More sharing options...
Mr.Key Posted October 6, 2012 Report Share Posted October 6, 2012 un visur jāizmanto fjas ar mb_ prefiksu, ja tādas ir. Piemēram: Ne vienmēr :) http://www.php.net/manual/en/mbstring.overload.php Quote Link to comment Share on other sites More sharing options...
Cibiņš Posted October 6, 2012 Author Report Share Posted October 6, 2012 Njā, paldies vsjem, šis $truncated_header = mb_substr($name, 0, $character_limit).$text_points; vajadzēja uz $truncated_header = mb_substr($name, 0, $character_limit, 'utf-8').$text_points; Quote Link to comment Share on other sites More sharing options...
Kavacky Posted October 8, 2012 Report Share Posted October 8, 2012 (edited) Vai arī visa skripta sākumā: mb_internal_encoding. Tad nevajag vislaik dot līdzi enkodingu. Edited October 8, 2012 by Kavacky Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.