Saturday 15 March 2014

PHP Regex match first newline after x characters for a trimming function -


I am writing a trimming function that takes the string and gets the first new line \ n characters After 500th character and returns a string to the new line, in fact, if I have \ n on the index of 200, 400 and 600, then I want to return 600 characters before the function string. ( \ n ).

I tried:

  $ output = preg_replace ('/ ([^%] {500} [^ \ n] +?) [^%] * / ',' $ 1 ', $ production);  

I used the percent mark because I could not find any square squares which simply included "ever anything" dot did not do this because it did not include new lines Unfortunately, my work fails badly, any help or guidance will be appreciated. Personally I would like to avoid regex and I will use simple string functions:

Pre> // $ str is the original string $ nl = strapo ($ str, "\ n", 500); // first starts with $ 500 sub $ substr ($ str, 0, $ nl); $ Final = str_replace ("\ n", '', $ sub); You may also need to check for \ r \ n - i.e. first str_replace ("\ r \ n", "\ n", $ Str) .


No comments:

Post a Comment