preg_replace help please
Posted: Mon May 11, 2009 8:12 pm
sorry guys im a php noob and i need help with this little bit of code here:
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
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();
}
?>
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