preg_replace function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

preg_replace function

Post 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?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: preg_replace function

Post 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...
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: preg_replace function

Post by papa »

Ok got it..

Code: Select all

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

Code: Select all

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