Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Preg_Replace - problem.
mcfreak
post 21.05.2010, 15:10:50
Post #1





Grupa: Zarejestrowani
Postów: 53
Pomógł: 0
Dołączył: 30.01.2009

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


witam, mam taki sobie kod:

  1. ...
  2. $w = '[q]{x}[/q]';
  3. $w = str_replace('{x}','(.*?)', $w);
  4. $w ="/\"".$w."/";
  5. $mx = preg_replace($w,$ow,$mx); # linia 16


jednak wywala błąd:

CODE
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'r' in ... on line 18


Co zrobić, żeby nie wywalało błędu? Jak skonstruować tą zmienną tak, aby działało:

  1. $w ="/\"".$w."/";


  1. Echo $w;
zwraca normalnie /\[q](.*?)[/q]/

McFreak.
Go to the top of the page
+Quote Post
tehaha
post 21.05.2010, 15:19:39
Post #2





Grupa: Zarejestrowani
Postów: 1 748
Pomógł: 388
Dołączył: 21.08.2009
Skąd: Gdynia

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


spróbuj zrobić żeby zwracało /\[q](.*?)[\/q]/
Go to the top of the page
+Quote Post
mcfreak
post 21.05.2010, 19:53:44
Post #3





Grupa: Zarejestrowani
Postów: 53
Pomógł: 0
Dołączył: 30.01.2009

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


czyli przed każdym / mam stawiać \? ok , sprawdzę

edit:

jednak nie podziałało
/\[q](.*?)[\/q]/ jest, a błąd ten sam

Odświeżam, potrzebne naprawdę!

Ten post edytował mcfreak 21.05.2010, 15:29:14
Go to the top of the page
+Quote Post
tehaha
post 21.05.2010, 20:10:15
Post #4





Grupa: Zarejestrowani
Postów: 1 748
Pomógł: 388
Dołączył: 21.08.2009
Skąd: Gdynia

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


daj taki kawałek kodu, żebym mógł u siebie zobaczyć
Go to the top of the page
+Quote Post
mcfreak
post 21.05.2010, 21:12:58
Post #5





Grupa: Zarejestrowani
Postów: 53
Pomógł: 0
Dołączył: 30.01.2009

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


  1. <?php
  2. $x = 'haha.gif [x]{val}[/x] haha.gif'; // tekst
  3. $w = '[x]{val}[/x]'; // wyszukiwane
  4. $ow = '<b>$1</b>'; // zamiana
  5.  
  6. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  7. // a tutaj caly proces zamiany, taki jak by system bbcode
  8. $w = str_replace('{val}','(.*?)', $w);
  9. $w ="/\\".$w."/";
  10. $x = preg_replace($w,$ow,$x);
  11. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  12. ?>


a błąd taki:

  1. Warning: preg_replace() [function.preg-replace]: Unknown modifier 'b' in ... on line 10


Ten post edytował mcfreak 21.05.2010, 21:15:12
Go to the top of the page
+Quote Post
croc
post 21.05.2010, 21:45:46
Post #6





Grupa: Zarejestrowani
Postów: 706
Pomógł: 108
Dołączył: 12.03.2010

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


Powinno być:
  1. $ow = '<b>$1<\/b>'; // zamiana
Go to the top of the page
+Quote Post
tehaha
post 21.05.2010, 21:48:07
Post #7





Grupa: Zarejestrowani
Postów: 1 748
Pomógł: 388
Dołączył: 21.08.2009
Skąd: Gdynia

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


na tym prostym przykładzie u mnie działa:
  1. $array_find = array('[', ']', '/', '{val}');
  2. $array_replace = array('\[', '\]', '\/', '([^-]*)');
  3.  
  4. $string = "xd [x]{val}[/x] haha.gif";
  5. $look_for = "[x]{val}[/x]";
  6.  
  7. $pattern = "/".str_replace($array_find, $array_replace, $look_for)."/";
  8. $replace = "<b>$1</b>";
  9.  
  10. echo preg_replace($pattern, $replace, $string);
Go to the top of the page
+Quote Post
mcfreak
post 21.05.2010, 22:19:42
Post #8





Grupa: Zarejestrowani
Postów: 53
Pomógł: 0
Dołączył: 30.01.2009

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


Dzięki, działa, jednak jest problem:

jeżeli $string: '***{val}***'; wywala błąd:

  1. Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0 in ... on line 24

linijka 24 to preg_replace
Go to the top of the page
+Quote Post
tehaha
post 21.05.2010, 22:30:22
Post #9





Grupa: Zarejestrowani
Postów: 1 748
Pomógł: 388
Dołączył: 21.08.2009
Skąd: Gdynia

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


$string czy $pattern?
  1. <?php
  2.  
  3. $array_find = array('[', ']', '/', '*');
  4. $array_replace = array('\[', '\]', '\/', '\*');
  5.  
  6. $string = "xd [x]***{val}***[/x] haha.gif";
  7. $look_for = "[x]{val}[/x]";
  8.  
  9. $pattern = "/".str_replace($array_find, $array_replace, $look_for)."/";
  10. $pattern = str_replace('{val}','([^-]*)',$pattern);
  11. $replace = "<b>$1</b>";
  12.  
  13. echo preg_replace($pattern, $replace, $string);
  14. ?>


generalnie problem leży w tym że używasz znaki o specjalnym znaczeniu dla preg_match, dlatego przez str_replace podmieniamy np [ na \[ , * na \* i tam w array_find i array_replace musisz wstawić dla każdego znaku specjalnego taką podmianę
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 23.07.2025 - 02:22