title: Regex to truncate string on closest expression/space with length limit
body: <p>I want to get this results (from -> to)</p>
  
  <pre><code># use string length limit = 3
  1 {2 3}       -&gt; 1 # the string between the {} must be whole
  1 2 3         -&gt; 1 2
  1 23          -&gt; 1
  {1}           -&gt; {1} 
  {1 2}         -&gt; empty 
  123456        -&gt; 123 # if there is no spaces, cut string by symbols (except {*} expressions). Not necessarily but it would be cool
  
  # one more example. Use string length limit = 5
  {1} 2           -&gt; {1} 2
  123 45          -&gt; 123
  123 4           -&gt; 123 4
  </code></pre>
  
  <p>Is there a way to do this using PHP with one regex expression?</p>
  
  <p>Length limit may be dynamic.</p>
  
  <p>Similar question - <a href="https://stackoverflow.com/questions/972010/get-first-100-characters-from-string-respecting-full-words">Get first 100 characters from string, respecting full words</a> (but my question requires full contain {*} expressions )</p>
  
  <p>I tried: <code>^(.{1,3})({.*}|\s|$)</code></p>