---
title: "preg_replace: <line=red> => <hr color=red>"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/109913-preg-replace-line-red-hr-color-red"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "Qonos"
published: "2004-08-24T08:21:46.000Z"
updated: "2004-08-24T10:27:19.000Z"
replies: 5
views: 370
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/109913-preg-replace-line-red-hr-color-red"
---

# preg_replace: <line=red> => <hr color=red>

## #1 — Qonos, 2004-08-24T08:21Z

Godmorgon, sitter med ett preg_replace problem. jag kommer nog aldrig att fatta detta helt men jag har fått stora delar av min funktion att fungera, men jag har stött på ett problem som jag gärna skulle vilja ha hjälp med..   
 
**detta har jag** 
Kör detta i en preg_replace:

```php
"/\[line=([0-9A-F#]+)\]/is",
```

 

```php
"<hr color=\"$1\">",
```

 

men när jag skriver

```php
[line=red]
[line=#0000ff]
[line]
```

 

så är det bara

```php
[line=#0000ff]
```

 

som fungerar resten ignoreras

**och så min fråga** 
Hur gör jag för att få den att använda även t.ex. "red" som färg samt att göra en \<hr\> i en default färg om inget anges?

Är det någon som har en länk till en bra sida för en nybörjare som mig på regexp? gärna svensk, dock inget krav iofs men... :)

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

## #2 — Hansen, 2004-08-24T09:25Z

Re: preg_replace: &lt;line=red&gt; =&gt; &lt;hr color=red&gt;

> **Qonos skrev:**
>
> ```php
> "/\[line=([0-9A-F#]+)\]/is"
> ```

Testa med:

```
"/\[line=(#?[0-9A-F]+)\]/is"
```

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

## #3 — dectgap, 2004-08-24T09:57Z

Eller:

```
/\[line=(#(?:[0-9A-F]{3}){1,2}|[a-z])*\]/is
```

Första parentesen ska matcha en färg uttryckt #RGB eller #RRGGBB, och andra parentesen matchar eventuell färgkod, skriven med bokstäverna a-z.

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

## #4 — Hansen, 2004-08-24T10:19Z

> **dectgap skrev:**
>
> ```
> /\[line=(#(?:[0-9A-F]{3}){1,2}|[a-z])*\]/is
> ```
>
> \[...\]

Saknar ett plus för att matcha färgnamn:

```
/\[line=(#(?:[0-9A-F]{3}){1,2}|[a-z]+)*\]/is
```

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

## #5 — Qonos, 2004-08-24T10:19Z

dectgap

tackar den tog de olika färgerna men \[line\] fungerade inte.. går det att på nått sätt sätta en default färg om användaren inte angett en färg?

förstod inte mycket av din kod, så nu får google arbeta lite för mig igen så jag kan få förklaring vad de olika delarna gör :)

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

## #6 — Hansen, 2004-08-24T10:27Z

> **Qonos skrev:**
>
> \[...\] nu får google arbeta lite för mig igen så jag kan få förklaring vad de olika delarna gör :)

```
The regular expression:

(?-imsx:\[line=(#?[A-Z0-9]+)\])

matches as follows:
  
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  \[                       '['
----------------------------------------------------------------------
  line=                    'line='
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    #?                       '#' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    [A-Z0-9]+                any character of: 'A' to 'Z', '0' to '9'
                             (1 or more times (matching the most
                             amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \]                       ']'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
```

```
The regular expression:

(?-imsx:\[line=(#(?:[0-9A-F]{3}){1,2}|[a-z]+)*\])

matches as follows:
  
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  \[                       '['
----------------------------------------------------------------------
  line=                    'line='
----------------------------------------------------------------------
  (                        group and capture to \1 (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    #                        '#'
----------------------------------------------------------------------
    (?:                      group, but do not capture (between 1 and
                             2 times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      [0-9A-F]{3}              any character of: '0' to '9', 'A' to
                               'F' (3 times)
----------------------------------------------------------------------
    ){1,2}                   end of grouping
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [a-z]+                   any character of: 'a' to 'z' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )*                       end of \1 (NOTE: because you're using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
----------------------------------------------------------------------
  \]                       ']'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
```

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

---

Tråden på webben: https://www.webforum.nu/amne/php/109913-preg-replace-line-red-hr-color-red
