@@ -351,3 +351,66 @@ function splitWithExpression($haystack, $needle, $limit = 0)
351351
352352 return preg_split ($ needle , $ haystack , $ limit );
353353}
354+
355+ function trim ($ haystack , $ needle )
356+ {
357+ if (isExpression ($ needle )) {
358+ return trimWithExpression ($ haystack , $ needle );
359+ }
360+
361+ return trimWithString ($ haystack , $ needle );
362+ }
363+
364+ function trimWithString ($ haystack , $ needle )
365+ {
366+ return \trim ($ haystack , $ needle );
367+ }
368+
369+ function trimWithExpression ($ haystack , $ needle )
370+ {
371+ $ pattern = slice ($ needle , 1 , length ($ needle ) - 2 );
372+
373+ return preg_replace ("#^ {$ pattern }| {$ pattern }$# " , "" , $ haystack );
374+ }
375+
376+ function trimLeft ($ haystack , $ needle )
377+ {
378+ if (isExpression ($ needle )) {
379+ return trimLeftWithExpression ($ haystack , $ needle );
380+ }
381+
382+ return trimLeftWithString ($ haystack , $ needle );
383+ }
384+
385+ function trimLeftWithString ($ haystack , $ needle )
386+ {
387+ return ltrim ($ haystack , $ needle );
388+ }
389+
390+ function trimLeftWithExpression ($ haystack , $ needle )
391+ {
392+ $ pattern = slice ($ needle , 1 , length ($ needle ) - 2 );
393+
394+ return preg_replace ("#^ {$ pattern }# " , "" , $ haystack );
395+ }
396+
397+ function trimRight ($ haystack , $ needle )
398+ {
399+ if (isExpression ($ needle )) {
400+ return trimRightWithExpression ($ haystack , $ needle );
401+ }
402+
403+ return trimRightWithString ($ haystack , $ needle );
404+ }
405+
406+ function trimRightWithString ($ haystack , $ needle )
407+ {
408+ return rtrim ($ haystack , $ needle );
409+ }
410+
411+ function trimRightWithExpression ($ haystack , $ needle )
412+ {
413+ $ pattern = slice ($ needle , 1 , length ($ needle ) - 2 );
414+
415+ return preg_replace ("# {$ pattern }$# " , "" , $ haystack );
416+ }
0 commit comments