Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/187830-understanding-how-preg_replace-works/
Share on other sites

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.