Jump to content

The Attack of Preg_Replace (AGAIN!)


maxudaskin

Recommended Posts

I want it to get a proper URL for later use.

 

Source Code:

Constants.php

define ( "ROOTURL" , "http://www.virtualzoom.net/pirepdemo/" ); // Root URL

<?php
include("./constants.php");

$stylesheet; // Set Variable for Later Use

if(!is_readable("styles/style.txt")){
    echo "<strong>An Error Has Occured: Cascading Style Sheet Setup File is Corrupt or missing. Reverting to default style.</strong>"; // Output Warning
$stylesheet = ROOTURL . "include/styles/default/default.css"; // Set Style Sheet as Default
}else{
$style_text = preg_split("/\-\-\-/",implode(" ",file("styles/style.txt"))); // Get file contents and merge it; Split it up
$standard   = $style_text[1]; // Set The Standard Style Sheet
$secondary  = isset($style_text[2]) ? $style_text[2] : "default"; // Set The Backup Style Sheet (Optional)

$folder     = preg_replace("/\{folder_name\}\=\"(.*)\"/","$1",$standard); // Set the folder name
$file       = preg_replace("/\{file_name\}\=\"(.*)\"/","$1",$standard); // Set the file name

echo ROOTURL . $folder . "/" . $file . ".css";
echo "<br />";

if(is_readable(ROOTURL . $folder . "/" . $file . ".css")){ // If the specified file is readable
    $stylesheet = ROOTURL . $folder . "/" . $file . ".css"; // Set Style Sheet as Specified
}else{
    if($secondary == "default"){
	    $stylesheet = ROOTURL . "include/styles/default/default.css"; // Set Style Sheet as Default
	}else{
	    $folder = preg_replace("/\{folder_name\}\=\"(.*)\"/","$1",$secondary); // Set the folder name
        $file   = preg_replace("/\{file_name\}\=\"(.*)\"/","$1",$secondary); // Set the file name

echo ROOTURL . $folder . "/" . $file . ".css";
echo "<br />";

		if(is_readable(ROOTURL . $folder . "/" . $file . ".css")){
		    $stylesheet = ROOTURL . $folder . "/" . $file . ".css"; // Set Style Sheet as Specified
		}else{
		    $stylesheet = ROOTURL . "include/styles/default/default.css"; // Set Style Sheet as Default
		}
	}
}
}
echo $stylesheet;
?>

 

style.txt

This file was written by Max Udaskin.
Please refer to the users manual before attempting to edit this file.

---

<standard>{
{folder_name}="standard"
{file_name}="red"
}

---

<secondary>{
{folder_name}="standard"
{file_name}="white"
}

 

Output

http://www.virtualzoom.net/pirepdemo/ { {folder_name}="standard" {file_name}="red" } / { {folder_name}="standard" {file_name}="red" } .css

http://www.virtualzoom.net/pirepdemo/ { standard {file_name}="white" }/ { {folder_name}="standard" white }.css

http://www.virtualzoom.net/pirepdemo/include/styles/default/default.css

 

I think it is my parsing in Preg_Replace.

Link to comment
https://forums.phpfreaks.com/topic/113999-the-attack-of-preg_replace-again/
Share on other sites

Try this instead:

 

$standard = '{ {folder_name}="standard" {file_name}="red" }';
if (preg_match("/\{folder_name\}\=\"([^\"]*)\"/",$standard, &$matches)) {
   // Set the folder name
   $folder = $matches[1];
}
if (preg_match("/\{file_name\}\=\"([^\"]*)\"/",$standard, &$matches)) {
   // Set the file name
   $file = $matches[1];
}
print "Folder: $folder\n";
print "File: $file\n";

 

The problem with preg_replace() is that it will retain the unmatched portions of the original string, which isn't what you need here.

 

I also changed the regexp so it doesn't continue beyond the second double quote when parsing a single variable assignment.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.