---
title: "egen \"tag\" -> html"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/158896-egen-tag-html"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "jme"
published: "2007-04-03T14:24:28.000Z"
updated: "2007-04-03T17:49:45.000Z"
replies: 4
views: 548
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/158896-egen-tag-html"
---

# egen "tag" -> html

## #1 — jme, 2007-04-03T14:24Z

Hej.

Jag har följande kod:

```php
	 
$pattern = array("/\[link](.*?) (.+?)\[\/link]/is");
	   
$replace = array('<a href="$1">$2</a>');  
	   
$text = preg_replace($pattern, $replace, $text);
```

Tanken är att:

\[link\]http://www.google.fi\[/link\] -\> \<a href="http://www.google.fi"\>http://www.google.fi\</a\>

och

\[link\]http://www.google.fi google\[/link\] -\> \<a href="http://www.google.fi"\>google\</a\>

Jag får min kod att fungera antingen eller.

Användaren kan, om han/hon vill även ge en kortare titel åt länken men det krävs inte.

Permalänk: https://www.webforum.nu/p/158896

## #2 — voigtann1, 2007-04-03T14:46Z

Lägg till följande kod efter din befintliga kod:

```
$pattern = array("/\[link](.*?)\[\/link]/is");
       
$replace = array('<a href="$1">$1</a>');  
       
$text = preg_replace($pattern, $replace, $text);
```

Detta bör fungera med:

```
$pattern = array("/\[link](.*?) (.+?)\[\/link]/is","/\[link](.*?)\[\/link]/is");
$replace = array('<a href="$1">$2</a>','<a href="$1">$1</a>');  
$text = preg_replace($pattern, $replace, $text);
```

Permalänk: https://www.webforum.nu/p/1956231

## #3 — jme, 2007-04-03T16:18Z

Tyvärr, ingetdera verkar fungera sisådär perfekt.

```php
$text = "Bla bla blaargh: [link]http://www.apple.com[/link], [link]http://www.gaah.fi gaah[/link]";

$pattern = array("/\[link](.*?) (.+?)\[\/link]/is","/\[link](.*?)\[\/link]/is");
$replace = array('<a href="$1">$2</a>','<a href="$1">$1</a>');  
$text = preg_replace($pattern, $replace, $text);

echo $text; // Bla bla blaargh: [link]http://www.gaah.fi gaah
```

Permalänk: https://www.webforum.nu/p/1956260

## #4 — Peter S, 2007-04-03T17:18Z

Testa

```php
$pattern = array("/\[link](.*?)(?: (.+?))?\[\/link]/eis"); 

$replace = array("'<a href=\"$1\">' . (strlen('$2') > 0 ? '$2' : '$1') . '</a>'");

$text = preg_replace($pattern, $replace, $text);
```

Permalänk: https://www.webforum.nu/p/1956285

## #5 — jme, 2007-04-03T17:49Z

Tack!

Permalänk: https://www.webforum.nu/p/1956295

---

Tråden på webben: https://www.webforum.nu/amne/php/158896-egen-tag-html
