soycharliente Posted January 9, 2010 Share Posted January 9, 2010 After reading the manual, I've come to the conclusion that preg_replace() is the function I need to use for what I'm trying to do. I want to make sure that I understand how it works as well. I have an XML file with a tag containing multiple attributes. When trying to use the SimpleXML library I've found that those attributes cause the parsing to not work. (I don't know why. I randomly deleted them trying to figure out why the heck it wasn't working and it started working.) I'm trying to delete those attributes on teh fly just before the XML parsing starts. (The XML file is generated by a system that we don't have access to. We are basically told that we have to work with what we get, so I can't change the way the XML file comes back.) Do I understand the manual correctly in that the first match to the regex I enter will be replaced with the second parameter <tag>? $xml = preg_replace($regex,'<tag>',file_get_contents('file.xml'),1); Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/187830-understanding-how-preg_replace-works/ Share on other sites More sharing options...
JAY6390 Posted January 9, 2010 Share Posted January 9, 2010 Hi Charlie. Whatever your $regex pattern matches will be replaced with the <tag> in your example yes Quote Link to comment https://forums.phpfreaks.com/topic/187830-understanding-how-preg_replace-works/#findComment-991708 Share on other sites More sharing options...
soycharliente Posted January 9, 2010 Author Share Posted January 9, 2010 I'm getting an error for my pattern. How am I supposed to start the pattern then? The whole thing is supposed to search for the opening of the tag, followed by zero or more of a space or word character or = or " or : or . or / or %, followed by the closing of the tag. <?php $xml = preg_replace('\<tag[\s\w=":./%]*/\>','<tag>',file_get_contents('file.xml'),1); echo $xml; ?> Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in /html/path/to/file/test.php on line 2 Quote Link to comment https://forums.phpfreaks.com/topic/187830-understanding-how-preg_replace-works/#findComment-991749 Share on other sites More sharing options...
cags Posted January 9, 2010 Share Posted January 9, 2010 There was recently a page added to the manual specifically to explain delimiters. Quote Link to comment https://forums.phpfreaks.com/topic/187830-understanding-how-preg_replace-works/#findComment-991884 Share on other sites More sharing options...
soycharliente Posted January 10, 2010 Author Share Posted January 10, 2010 <?php $xml = preg_replace('#\<tag[\s\w=":/.%]*\>#', '<tag>', file_get_contents('file.xml'), 1); ?> Awesome. My regex worked first try once I got the delimiter issue worked out. Thanks for the link. Quote Link to comment https://forums.phpfreaks.com/topic/187830-understanding-how-preg_replace-works/#findComment-992064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.