Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [XML][PHP] otwarcie xml, edycja, zapisanie do pliku
neurogen
post
Post #1





Grupa: Zarejestrowani
Postów: 58
Pomógł: 0
Dołączył: 25.03.2018

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


ten kod zapisuje mi plik i dokonuje zmian str_replace ale nie działa preg_replace, gdzie jest błąd?
  1. <?php
  2. $fname = "pubmed_result.xml";
  3. $fhandle = fopen($fname,"r");
  4. $content = fread($fhandle,filesize($fname));
  5.  
  6. $content = preg_replace('/<ISSN IssnType=\'Print\'>.*?<\/ISSN>/', '', $content);
  7. $content= str_replace('Label="Introduction"', 'Label="Background"', $content);
  8.  
  9. $savename = "pubmed_ready.xml";
  10. $fhandle = fopen($savename,"w");
  11. fwrite($fhandle,$content);
  12. fclose($fhandle);
  13. ?>


próbowałem też:
  1. $content = preg_replace('<ISSN IssnType="Print">.*?</ISSN>', '', $content);


Ten post edytował neurogen 7.06.2018, 19:09:41
Go to the top of the page
+Quote Post
SmokAnalog
post
Post #2





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

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


Pierwszy przykład powinien działać. Czy wewnątrz tagu są znaki nowej linii? Jeśli tak, to musisz dać flagę s.

Cytat z dokumentacji flag wyrażeń regularnych w PHP:
Cytat
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.


Czyli to powinno wtedy działać:
  1. $content = preg_replace('/<ISSN IssnType=\'Print\'>.*?<\/ISSN>/s', '', $content);


Jeśli dalej nie działa, to gdzieś się kopnąłeś we wzorcu.
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 Aktualny czas: 22.08.2025 - 03:53