Page 1 of 1

preg_replace function

Posted: Fri Sep 19, 2008 4:31 am
by papa
Good day,

I'm doing a simple text validator function that replaces a couple of characters. I wanted to add a function that replaces links and emails with <a href="">.

In my text in order to create a link the user types: linkto:mypage.php<description> or mailto:bla@bla.com<mr bla>

I'm not sure how to go about this though:

Code: Select all

        //patterns to replace
        $patterns = array("/Å/", "/å/", "/Ä/", "/ä/", "/Ö/", "/ö/", "/linkto:[b][a-zA-Z0-9._-][/b]+<[b][a-zA-Z0-9._-\s][/b]+>/");
        //replacements
        $replacements = array("&Aring;", "&aring;", "&Auml;", "&auml;", "&Ouml;", "&ouml;", "<a href=\"[b]bla[/b]\">[b]bla2[/b]</a>");
 
Is there a way to get my bolded code where bla and bla2 is?

Re: preg_replace function

Posted: Fri Sep 19, 2008 5:53 am
by papa
//patterns to replace
$patterns = array("/Å/", "/å/", "/Ä/", "/ä/", "/Ö/", "/ö/", "/linkto:([a-zA-Z0-9._-]+)<([a-zA-Z0-9._-\s]+)>/");
//replacements
$replacements = array("&Aring;", "&aring;", "&Auml;", "&auml;", "&Ouml;", "&ouml;", "<a href=\"".${1}."\">"".${2}."</a>");
Tried adding the reference stated in the function definition on php.net but can't get it to work...

Re: preg_replace function

Posted: Fri Sep 19, 2008 5:58 am
by papa
Ok got it..

Code: Select all

"<a href=\"".${1}."\">"".${2}."</a>");
Should be:

Code: Select all

"<a href=\"$1\">$2</a>");