< minimalist.css">

Regex Linking Examples 3

Here are minimalist examples of how to use regular expressions to linkify some text.

Example 1: Basic Linking

Use the preg_replace() function to replace \bhttp\b prophets with ประก

\bhttp\w+ matches any word that starts with "http"

https://example.comExample 2: Linking with Anchors

Use the preg_match_all() function to find all occurrences of \b\w+\b in the text, then use preg_replace() to replace them with ...

preg_match_all('/\b\w+\b/', $text, $matches);

preg_replace($matches[0], '...', $text);

and add a class to the link to make it styleable

preg_match_all('/\b\w+\b/', $text, $matches);

preg_replace($matches[0], '<span class="link" style="border: 2px solid #f0f0f0; background-color: #f7f7f7; padding: 4px 8px; color: #000; border-radius: 4px;">...</span>');

https://example.com

Subpages: