+4 votes
in Programming Languages by (59.8k points)
I have strings that contain both English and non-English substrings. Some of the non-English substrings are very big and when I try to display those strings on a mobile device, they do not fit in the screen area. I want to split bigger non-English substrings so that strings will not go beyond the screen area. How can I split substrings in a string?

1 Answer

+1 vote
by (353k points)
selected by
Best answer

Here is a sample PHP code that can be used to split non-English substrings of a string. After the split, substrings may contain some junk characters. This code splits the substring by length and here I am using 25 as the new length of substrings. If the original length of a substring is less than 25, it will not be split.

  $title_string = "Mahadevi 4K MGR, சாவித்திரி ஈடு இணையில்லா நடிப்பில் மகாதேவி இப்பொழுது 4K யில்";
  if(strlen($title_string) != mb_strlen($title_string, 'utf-8'))
    $mobile_len = 25; // size of new substrings
    $modified_title = array();
    $j = 0;
    $vals = explode(" ", $title_string);
    $arr_size = sizeof($vals);
    for($k = 0; $k<$arr_size; $k++){
      if(strlen($vals[$k]) > $mobile_len){
      $substrings = str_split($vals[$k], $mobile_len);
      $modified_title[$j] = implode(" ", $substrings);
      $modified_title[$j] = $vals[$k];
  $vidTitle = implode(" ", $modified_title);
  else {
    $vidTitle = $title_string;
  echo "Original: " . $title_string . "\n";
  echo "Modified: " . $vidTitle . "\n";

The output of the above code is:

Original: Mahadevi 4K MGR, சாவித்திரி ஈடு இணையில்லா நடிப்பில் மகாதேவி இப்பொழுது 4K யில்
Modified: Mahadevi 4K MGR, சாவித்தி� ��ி ஈடு இணையில்ல� �� நடிப்பில� �� மகாதேவி இப்பொழுத� �� 4K யில்