1

I got this little problem here with my website. I want to replace a string in one of my html file. I did a php file that find the specific string and replace it but it become useless become the string to change is not the same after...

In my HTML file, I'm able to change 7NT0j5dB2QMyWv96nXIDgaR4PJ for whatever random string the PHP file generate

<tr>
<td>Secret Key</td>
<td><strong style="color:#b94a48;">7NT0j5dB2QMyWv96nXIDgaR4PJ</strong></td>
</tr>

But in my PHP file the string to replace still 7NT0j5dB2QMyWv96nXIDgaR4PJ not the new one...

There is my php code :

$contents = file_get_contents ("step-second-test.php");

$contents = str_replace(array("7NT0j5dB2QMyWv96nXIDgaR4PJ"), generateRandomString($length = 26), $contents);
file_put_contents("step-second-test.php", $contents);
11
  • Why not just directly echo the result of the function in step-second-test.php ? Commented Sep 12, 2017 at 19:15
  • 1
    Works for me 3v4l.org/R1K1d Maybe the problem is in generateRandomString, which you didn't include Commented Sep 12, 2017 at 19:16
  • ^ This means that what are you trying to replace does not exist in a file. OR this html part is created dynamically Commented Sep 12, 2017 at 19:17
  • @u_mulder OP's point is that file_put_contents("step-second-test.php", $contents); changes the file, meaning it is basically useless after the first run. Commented Sep 12, 2017 at 19:18
  • generateRandomString($length = 26) probably should be generateRandomString(26). Can you also post that function? Commented Sep 12, 2017 at 19:26

2 Answers 2

2

To me, it's just a matter of moving the function call from wherever it is not to be directly in step-second-test.php

<tr>
<td>Secret Key</td>
<td><strong style="color:#b94a48;"><?php echo generateRandomString(26);
 ?></strong></td>
</tr>

Just make sure you also have the function definition in this file, or have an include() to the file that has the function definition.

Sign up to request clarification or add additional context in comments.

1 Comment

@jamesbub don't forget to upvote and/or accept if you like the answer. +1 from me
0

As I understand you use step-second-test.php as static file which changes over time. To handle your problem you either convert you step-second-test.php to dynamic file like this:

<tr>
<td>Secret Key</td>
<td><strong style="color:#b94a48;"><?php echo generateRandomString(26); ?></strong></td>
</tr>

And from now on you include 'step-second-test.php'; in your php scripts instead of using file_get_contents. Remember, though, that generateRandomString needs to be accessible.

If you want to stick to what you have you need to change your thinking. Problem is not replacing "7NT0j5dB2QMyWv96nXIDgaR4PJ" with whatever else but replacing whatever is between <strong> tags with whatever else.

With problem stated as such there are many options the easiest of which I find using regex. PHP makes the problem easier by providing preg_replace. So solution to your problem would be:

$contents = file_get_contents ("step-second-test.php");

$pattern = '/(.*<strong.*>).*(<\/strong>.*)/i';
$replacement = '${1}' . generateRandomString($length = 26) . '${2}';
$contents = preg_replace($pattern, $replacement, $contents);

file_put_contents("step-second-test.php", $contents);

If you don't know regular expressions I recommend regexr.com for learning and playing around with them. Regular expressions are great thing. Mandatory for any programmer. Nice to know for any computer user.

3 Comments

Thanks Alek! Good answer too ! :)
FYI, this will replace all instances of text within <strong></strong> tags. For the limited example given above, that may be fine, but on a larger scale, using regex to parse HTML is rarely the way to go.
I use the solution of @PatrickQ and it's working perfectly fine! Thanks again guys!

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.