petrogromovo Posted May 8, 2023 Share Posted May 8, 2023 Hi all, I need to make an array from string by more 1 space. I do it with code: $subject = " Line one Line Two Line Three LineLast "; $result = preg_match_all("~(.+)\b(?:[\s]{2,}|$)~U", $subject, $matches); It works, but I need to trim all elements inside of $matches : $matches = preg_replace_callback($validPattern, array($this, "trimStringValue"), $subject, limit: -1); public function trimStringValue($matches) { return trim($matches[1]); } But value of $matches after preg_replace_callback has concatanated string of all arrays in $subject, but not array of trimmed lines as I need. Why so and how that can be fixed ? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/316271-why-value-returned-in-preg_replace_callback-is-string/ Share on other sites More sharing options...
Solution kicken Posted May 8, 2023 Solution Share Posted May 8, 2023 The function you want is preg_split, not preg_match_all or preg_replace_callback. Quote Link to comment https://forums.phpfreaks.com/topic/316271-why-value-returned-in-preg_replace_callback-is-string/#findComment-1608188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.