Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting a string by concatenating another string

Tags:

php

There are hundreds of places in my project which contain the following code:

include $root . $template;

Where $root is document root for the relevant part of the CRM and $template is the HTML template file. The $root variable is different for different parts of the CRM.

I want to now make all parts of the CRM use the same template file. I can't simply change $root to the central document root because there are other places in the code which use that variable. I just want to change it for loading the template.

Is there a way to save time, so I don't have to go over all the places in my code where this appears? Is there perhaps a way of making the $template string overwrite the $root string when concatinated?

i.e.

$root="$_SERVER['document_root']."\some_folder";
$template="[something_to_ignore_concatination?]".$_SERVER['document_root']."\template_file";
//So $root.$template = just $template
like image 910
dlofrodloh Avatar asked Apr 11 '17 15:04

dlofrodloh


People also ask

Can you use += for string concatenation?

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

What happens when concatenate two strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

Can we concatenate two strings?

Syntax: string new_string = string init + string add; This is the most easiest method for concatenation of two string. The + operator simply adds the two string and returns a concatenated string.


1 Answers

Warning: What you'll see below is an abomination, do not use this in real software!

Assuming these values:

$_SERVER['document_root'] = 'path/to/root'

$root == $_SERVER['document_root'] . '/some_folder' == 'path/to/root/some_folder'

$template == '/' . $_SERVER['document_root'] . '/file.php' == '/path/to/root/file.php'

$root . $template == 'path/to/root/some_folder/path/to/root/file.php'

You could symlink

path/to/root/some_folder/path/to/root -> path/to/root/

So that the double path still gets resolved.

Note: The paths above should be treated as relative to one of the folders added to the PHP include_path.

like image 137
Alin Purcaru Avatar answered Oct 25 '22 00:10

Alin Purcaru