How do I truncate a string with dynamic length url slug?
<p>On my <code>slug</code> column I set the limit is <code>varchar = 100</code> and the values will store like this</p>

<pre><code>how-do-i-truncate-a-string-with-dynamic-length-url-slug
</code></pre>

<p>I want the slug will be a <code>unique</code> key so I update the <code>slug</code> value with last auto increment id and new slug value is : </p>

<pre><code>how-do-i-truncate-a-string-with-dynamic-length-url-slug-10
</code></pre>

<p>So here the <code>last auto increment</code> will be a big number at the end..</p>

<pre><code>preg_replace('/\s+?(\S+)?$/', '', substr($slugwithautoincrement, 0, 101));
</code></pre>

<p>With this <code>preg_replace</code> it will <strong>remove</strong> my <code>auto increment number</code></p>

<p>Question :</p>

<p>How do I truncate the value fix to 100 characters without remove the last auto increment number?</p>

<p>So result should be :</p>

<pre><code>how-do-i-truncate-a-string-with-[....remove from here...]10
</code></pre>
