CJ190

Что читабельнее?

Apr 13th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. String string = "param1 param2 'param3 param3 param3' param4 ' param5 param5 param5 param5' param6 param7 'param8' 'param9'";
  2. Matcher matcher = Pattern.compile("((?<=')([^']*)(?='(\\s|$)+)|(?<=\\s|^)[^\\s']*(?=\\s|$))").matcher(string);
  3. while (matcher.find()) System.out.println(matcher.group());
  4.  
  5.  
  6. =======================================================================================================================================
  7.  
  8. <?php
  9. $result=array();
  10.  
  11. $string="param1 param2 'param3 param3 param3' param4 ' param5 param5 param5 param5' param6 param7 'param8' 'param9' param99par";
  12. var_dump(parseParams($string));
  13.  
  14. function parseParams($string){
  15. if(substr_count($string, "'") % 2!=0)trigger_error("String malformed", E_USER_ERROR);
  16. $all = explode(' ', $string);
  17. $curridx = 0; $i=-1; $into = false; $beforeInto=false;
  18. foreach($all as $item){
  19. if(substr($item,0,1)=="'"){
  20. $into=true;
  21. $curridx = $i+1;
  22. $result[$i+1] = '';
  23. }
  24. if(substr($item, strlen($item)-1,1)=="'") {
  25. $into=false;
  26. $beforeInto=true;
  27. }
  28.  
  29. $rf = trim($item, "'");
  30.  
  31. if(substr($rf, strlen($rf)-1,1)=="'")$rf=substr($rf,strlen($rf)-1);
  32.  
  33. if(!$into && !$beforeInto){
  34. $result[] = $rf;
  35. }elseif($into || $beforeInto){
  36. $beforeInto=false;
  37. if(substr($rf,0,1)==" ")
  38. $result[$curridx] .= $rf;
  39. else
  40. $result[$curridx] .= (strlen($result[$curridx])==0?'':' ') . $rf;
  41. }
  42.  
  43. $i++;
  44. }
  45. return $result;
  46. }
Add Comment
Please, Sign In to add comment