3v4l.org

run code in 300+ PHP versions simultaneously
<?php function trim_slug(string $slug, int $maxlen = 150): string { // check if trimming is required: if(strlen($slug) <= $maxlen) { return $slug; } $pattern = '/^(?<job>.+)(?<loc>-in-\d{5}-.*-.*-\d*)$/'; // $match will have 'job' and 'loc' named keys with the matched values preg_match($pattern, $slug, $match); // raw cut of job name to maximum length: $max_job_chars = $maxlen - strlen($match['loc']); $job_name = substr($match['job'], 0, $max_job_chars); // tidy up to last delimiter, if exists, instead of mincing words: if($last_delim = strrpos($job_name, '-')) { $job_name = substr($match['job'], 0, $last_delim); } return $job_name . $match['loc']; } $string = '/job/hello-this-is-my-job-posting-for-a-daycare-im-looking-for-in-91770-rosemead-california-12345'; echo trim_slug($string, 80);
Output for git.master, git.master_jit, rfc.property-hooks
/job/hello-this-is-my-job-posting-for-a-in-91770-rosemead-california-12345

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
105.34 ms | 405 KiB | 5 Q