3v4l.org

run code in 300+ PHP versions simultaneously
<?php // all keys must be lowercase $keywords_external_path = array( "sweet potato" => "food/sweet-potato", "potato salads" => "food/potato-salads", "potato" => "food/potato", ); $data = array( 'post_content' => 'Sweet potato some text then potato then more text and then potato salads' ); $base = 'http://domain.com/'; // only if this is useful $keywords_external = array_keys($keywords_external_path); krsort($keywords_external); // to quickly discards useless position (if you have many keywords): you can also do the same with the second letter $keywords_first_letter = implode('', array_unique(array_reduce($keywords_external, function ($c, $i) { $c[]=$i[0]; return $c; }, []))); $pattern = '~\b(?=['. $keywords_first_letter . '])(?:' . implode('|', $keywords_external) . ')\b~i'; $result = preg_replace_callback($pattern, function ($m) use ($keywords_external_path, $base) { return '<a href="' . $base . $keywords_external_path . '">' . $m[0] . '</a>'; }, $data['post_content']); echo $result;
Output for git.master, git.master_jit, rfc.property-hooks
Warning: Array to string conversion in /in/Ye8ec on line 25 Warning: Array to string conversion in /in/Ye8ec on line 25 Warning: Array to string conversion in /in/Ye8ec on line 25 <a href="http://domain.com/Array">Sweet potato</a> some text then <a href="http://domain.com/Array">potato</a> then more text and then <a href="http://domain.com/Array">potato</a> salads

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:
33.55 ms | 402 KiB | 8 Q