Page 1 of 1

preg_replace help please

Posted: Mon May 11, 2009 8:12 pm
by wvucouchburner
sorry guys im a php noob and i need help with this little bit of code here:

Code: Select all

<?php
# Set Values
$my_file = 'mlb1.xml';
$new_value = check_input($_POST['url'], "Enter URL of stream");
# Get file contents
$xml = file_get_contents($my_file);
# Regex
$xml = preg_replace('<file>"(.*)"</file>', "file=\"$new_value\" /file", $xml);
# Write file out again
file_put_contents($my_file, $xml);
/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}
 
function show_error($myError)
{
?>
    <html>
    <body>
 
    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>
 
    </body>
    </html>
<?php
exit();
}
?>
 
This code takes a URL entered into an html form on another page and uses the value to overwrite a value in a separate xml document, i am having a bit of trouble with the preg_replace syntax trying to replace a value from inside <file></file> in the xml document with the new URL
If you guys could show me the way or correct me that would be great. also if you correct it and u run across any other errors please help me with those as well. thanks

Re: preg_replace

Posted: Mon May 11, 2009 8:48 pm
by wvucouchburner
also, at the part where it says "Enter URL of Stream" what exactly is supposed to be in that part of the code. that was what somebody else had told me to put there but it gives me an error reading: "Please correct the following error: Enter URL of stream" so im not sure what to make of it anymore :(

Re: preg_replace help please

Posted: Tue May 12, 2009 11:14 am
by wvucouchburner
can anybody help me with this please? :)