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

Link to comment
Share on other sites

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.