0

I want to edit string of variable. But I can't make it good. The code:

$mysite = 'mysite'; //name of site
$myurl = 'http:/mysite.com'; //url of website
$metaauthor = ''; // meta name author;
$metatitle = 'Online CV Builder'; // meta name title
$metacopy = 'Javid Karimov'; // meta name copyright
$metakeywords = 'CV, PDF, ONLINE, RESUME'; // meta name keywords
$allowcv = '2'; // how many cv's user can create
$keyword = 'cv, pdf, resume, create cv online'; // keyword for creating security key
$mailto = '[email protected]'; // send to mail in contact php

I change like this(IT WORKS):

$str = file_get_contents('../sys/sets.php');

//replace something in the file string
$str = str_replace($mysite, $sitenamexx, $str);
$str = str_replace($myurl, $siteurlxx, $str);
$str = str_replace($metaauthor, $metaavtorxx, $str);
$str = str_replace($metacopy, $metacopyxx, $str);
$str = str_replace($metatitle, $metatitlexx, $str);   
$str = str_replace($metakeywords, $metakeywxx, $str);
$str = str_replace($allowcv, $allowcvxx, $str);
$str = str_replace($keyword, $keywxx, $str);
$str = str_replace($mailto, $mailtoxx, $str);

//write string
$wok = file_put_contents('../sys/sets.php', $str);

if($wok) {
     echo 'Changed<br/>';
 }
 else {
     echo 'Error<br/>';
 }  

The question: If the string is empty (for example: $mysite = ''; ) then it does not change. How can I solve it ? May be there is another way ? I tried the way with fopen(), fgets(), and that way I have to use array, but I was not able to use it.

8
  • editing a php script with php is usually a bad idea Commented Sep 9, 2015 at 20:17
  • Ok, if it is a bad idea, what advice can you give ? Commented Sep 9, 2015 at 20:18
  • I don't think trying to search a non-empty string for an empty string is ever going to work Commented Sep 9, 2015 at 20:19
  • you mean I can't change it with any ways ? Commented Sep 9, 2015 at 20:20
  • My brain >.< Ok, wait, do you mean to update $thing to $thingxx? Or '$thing' to '$thingxx'? Commented Sep 9, 2015 at 20:22

1 Answer 1

1

If you've got an empty string and try to replace it, it's not going to work, as you've found. This is because you're trying to replace nothing with something. As such, it would either do nothing (which it does, and is sensible) or replace every instance of nothing in the file with something, i.e. repeatedly add that something to the file until memory ran out.

To do what you're after, you should try and replace the full line, so something like:

$str = str_replace('$mysite = \'' . $mysite . '\';', '$mysite = \'' . $sitenamexx . '\';', $str);
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Gabe3886. It worked. But I could not understand why it is a bad idea to edit php line with php ?
It's a bad idea to edit a php file with PHP because you could end up easily creating a syntax error which would lead to a fatal error every time a file is called which includes or requires that file. You'd have no idea that it happened until you found and saw the error. As others suggested, a json string or serialised array in a text file could work for you (check out json_encode and json_decode. If you go that route, make sure the file can't be loaded in the web browser by any visitor though or you could be opening up a huge security hole
but I deny that file in .htaccess . It will not help, yes ?
denying the file in .htaccess will help, yes. I had to state is as people will read these comments, put their config in a text file (probably config.txt) and they will be the ones who use root as their database user, and have it set up to connect from anywhere. Their database will be stolen and they will wonder why
if I undertand right you mean that it is bad idea. and better to use json or serialise array. ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.