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.
source share