<?php // INPUT: appl | pinea | orang frui | vege lates // TARGET: +(appl* | pinea* | orang*) +(frui* | vege*) +(lates*) $subject = 'appl | pinea | orang frui | vege lates'; // Add the "*" in place at end $subject = preg_replace('/[a-z]+/i', '$0*', $subject); // Group the terms adding brackets and + sign $result = preg_replace('/(?:[^\s|]+\s*\|\s*)*(?:[^\s|]+)/i', '+($0)', $subject); var_dump($result);
You have javascript disabled. You will not be able to edit any code.