How can I delete all the text after the point, while leaving the text to the point? It should be borne in mind that there may not be a point in the text, then you do not need to trim anything. Symbols can be both Cyrillic and Latin, using utf8.
- php.net/manual/ru/function.mb-strstr.php - Mike
- Need to leave a point? Or anyway? - cheops
- @cheops yes, you need it, but as far as I understand it, if you use the function proposed by Mike, you will get something like this: $ var = strstr ($ var, '.', true); $ var = $ var. '.'; - misc
|
3 answers
$str="тестовая строка"; $a=mb_strstr($str,".",true); if($a) $str=$a."."; print $str; Option a bit faster than using regular expressions.
|
You can do the following if you want to save the point
<?php $text = 'до точки.после точки'; echo preg_replace('/(?<=\.).*$/u', '', $text); and so if it is not needed
<?php $text = 'до точки.после точки'; echo preg_replace('/\..+$/u', '', $text); - 3
(?<=\.).*$requires 13 steps of the regularizer for the string "123.456". plus 2 steps for each character. Similar\.\K.*$- only 5 steps, since just runs along the line, searches for a point, steps over it and takes everything to the end of the line, backtracking is excluded - Mike - Accepted, thank you. - cheops
|
I will offer another option:
$str = 'Lorem ipsum dolor sit amet. Consectetur adipiscing elit.'; $str = explode( '.', $str )[0]; echo $str; http://sandbox.onlinephpfunctions.com/code/83113a892cd76b51359a280630890e247c851b88
|