Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] odwołanie do funkcji w preg_replace
-Daxis-
post
Post #1





Goście







Cześć
Mam takie oto wyrażenie:
  1. $post = preg_replace("#\[COLOR=rgb\((.*)\)\]#", "[color=\#" . $this->rgb2hex($1) . " . "\"]", $post);

Niestety wygląda na to że nie mogę użyć $1 odwołując się do funkcji rgb2hex, może mi ktoś jakoś pomóc to naprawić? (IMG:style_emoticons/default/Lkingsmiley.png)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
mortus
post
Post #2





Grupa: Zarejestrowani
Postów: 2 178
Pomógł: 596
Dołączył: 25.09.2009
Skąd: Piwniczna-Zdrój

Ostrzeżenie: (0%)
-----


Skorzystaj z preg_replace_callback():
  1. $post = preg_replace_callback("#\[COLOR=rgb\((.*)\)\]#",
  2. create_function(
  3. '$matches',
  4. 'return "[color=#{$this->rgb2hex($matches[1])}]";'
  5. ),
  6. $post);

Chyba jakoś tak.

Choć wydaje mi się, że problemem może być wyrażenie regularne.

Wcześniej spróbuj zamiast $this->rgb2hex($1) użyć $this->rgb2hex(\1).

Ten post edytował mortus 30.08.2011, 20:58:54
Go to the top of the page
+Quote Post
-Daxis-
post
Post #3





Goście







Cytat(mortus @ 30.08.2011, 21:11:47 ) *
Skorzystaj z preg_replace_callback():
  1. $post = preg_replace_callback("#\[COLOR=rgb\((.*)\)\]#",
  2. create_function(
  3. '$matches',
  4. 'return "[color=#{$this->rgb2hex($matches[1])}]";'
  5. ),
  6. $post);

Chyba jakoś tak.

Choć wydaje mi się, że problemem może być wyrażenie regularne.

Wcześniej spróbuj zamiast $this->rgb2hex($1) użyć $this->rgb2hex(\1).


Niestety zamiana $1 na \1 nie pomaga, dalej wywala błąd, natomiast to co podałeś z preg_replace_callback wypluwa
Kod
Fatal error: Using $this when not in object context


Cytat(Magic WWW @ 30.08.2011, 21:17:44 ) *
Możesz również użyć modyfikatora e.

  1. $post = preg_replace('#\[COLOR=rgb\((.*)\)\]#e', '[color="#$this->rgb2hex($1)"]', $post);



Kod
Niestety to również nie działa
[code]Parse error: syntax error, unexpected '['

Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 4.10.2025 - 17:08