How to count the first 30 letters in a line that ignores spaces

I want to take a description of the publication, but only display the first, for example, 30 letters, but ignore any tabs or spaces.

$msg = 'I only need the first, let us just say, 30 characters; for the time being.'; $msg .= ' Now I need to remove the spaces out of the checking.'; $amount = 30; // if tabs or spaces exist, alter the amount if(preg_match("/\s/", $msg)) { $stripped_amount = strlen(str_replace(' ', '', $msg)); $amount = $amount + (strlen($msg) - $stripped_amount); } echo substr($msg, 0, $amount); echo '<br /> <br />'; echo substr(str_replace(' ', '', $msg), 0, 30); 

The first output gives me "I only need the first, say, 30 characters, and the second output gives me: Ionlyneed the first, letusjustsay , so I know that this does not work as expected.

My desired result in this case would be:

 I only need the first, let us just say 

Thanks in advance, my math sucks.

+6
source share
5 answers

You can get the part with the first 30 characters with a regular expression:

 $msg_short = preg_replace('/^((\s*\S\s*){0,30}).*/s', '$1', $msg); 

Given a value of $msg you will end up in $msg_short :

I only need the first, let's just say

Regular expression explanation

  • ^ : match must begin at the beginning of a line
  • \s*\S\s* non-white space ( \S ) surrounded by zeros or more space characters ( \s* )
  • (\s*\S\s*){0,30} repeat the search for this sequence up to 30 times (greedy, as much as possible within this limit)
  • ((\s*\S\s*){0,30}) parentheses make up this group of characters number 1, which can be referenced as $1
  • .* any other characters. This will match all other characters due to the s modifier at the end:
  • s : makes matching dots with newline characters

Only characters that belong to the first group ( $1 ) are saved in the replacement. Everything else is ignored and not included in the returned string.

+5
source

Spontaneously, there are two ways to achieve what I can think of.

The first is close to what you have already done. Take the first 30 characters, count the spaces and take as many of the following characters as you found spaces, until the new letter set no longer contains spaces.

 $msg = 'I only need the first, let us just say, 30 characters; for the time being.'; $msg .= ' Now I need to remove the spaces out of the checking.'; $amount = 30; $offset = 0; $final_string = ''; while ($amount > 0) { $tmp_string = substr($msg, $offset, $amount); $amount -= strlen(str_replace(' ', '', $tmp_string)); $offset += strlen($tmp_string); $final_string .= $tmp_string; } print $final_string; 

The second method is to blow your string in spaces and combine them one by one until you hit your threshold (where you end up breaking one word into characters).

+3
source

Try this if it works:

 <?php $string= 'I only need the first, let us just say, 30 characters; for the time being.'; echo "Everything: ".strlen($string); echo '<br />'; echo "Only alphabetical: ".strlen(preg_replace('/[^a-zA-Z]/', '', $string)); ?> 
0
source

It can be done like this.

 $tmp=str_split($string);//split the string $result=""; $i=0;$j=0; while(isset($tmp[$i]) && $j<30){ if(trim($tmp[$i])){//test for non space and count $j++; } $result .= $tmp[$i++]; } print $result; 
0
source

I don't know the regex too well, so ...

 <?php $msg = 'I only need the first, let us just say, 30 characters; for the time being. Now I need to remove the spaces out of the checking.'; $non_space_hit = 0; for($i = 0; $i < strlen($msg); ++$i) { echo $msg[$i]; $non_space_hit+= (int)($msg[$i] !== ' ' && $msg[$i] !== "\t"); if($non_space_hit === 30) { break; } } 

The result is:

I only need the first, let's just say

0
source

Source: https://habr.com/ru/post/1012433/


All Articles