I want to replace the words(excluding: , ; . stc.) with links. How can I do this?
<?php
$string = "wordey; string, boom";
$string = preg_replace("/[^a-z]/i", "<a href='x'>/[^a-z]/i</a>", $string); //??
echo $string; // <a href='wordey'>wordey</a>; <a href='string'>string</a>, <a href='boom'>boom</a>
?>
Please note that the ; , . – etc are important.
Neither your regular expression or the replacement string make sense. The regular expression is matching everything not in the range
[a-z](denoted by the leading^), and your replacement string appears to contain regular expression syntax, which it shouldn’t.If you’re trying to replace the words, your regular expression should probably look something like
/[a-z]+/iwhich does a case-insensitive greedy match for one or more letters.To use the matched string in the replacement, you can use
\N, whereNis a number indicating the sub-match you want to reference. To add a sub-match, place brackets around the part of the regular expression you’re interested in referencing. The regex becomes/([a-z]+)/i.Put them together and you get the following, which appears to give the output you’re looking for.
Note the double-backslash is an escape sequence inserting a literal backslash into the string.