0

I'm running a reverse proxy on an Apache server. Using PHP, I'm trying to replace a string in the html of one of the pages being proxied with another string.

I added the following line to the apache2.conf in order to do the filtering:

ExtFilterDefine proxyfilter mode=output cmd="/usr/bin/php proxyfilter.php"

Here is the proxyfilter.php file referenced above that is doing the filtering:

<?php
$html = file_get_contents('php://stdin');

//solution 1 fails
$start=strpos($html,"<div id="id1" class="class1" >");
$end=strpos($html,"</div>");
$html=substr($html,0,$start+strlen('<div id="id1" class="class1" >') . "new-string" . substr($html,$end);

//solution 2 fails
$html = preg_replace('/<div id="id1" class="class1" >(.*?)<\/div>/', '<div id="id1" class="class1" >"new-string"</div>', $html);

file_put_contents('php://stdout', $html);
?>

The actual HTML that needs to be filtered (as it appears when you view the page in a browser and do view source) looks like:

<div id="id1" class="class1" >
 string-to-be-replaced     </div>

I'm running into two problems:

  1. The string I am trying to replace changes frequently.
  2. The string is located in between two <div> tags but there's also a bunch of extra spaces and a line break right before/after the string inside the <div> tags.

Looking at similar questions on Google & StackOverflow I was able to deduce two possible ways of going about this:

  1. Using preg_replace and regex to search for the unique <div> tag and then use wildcards for the text that needs to be replaced- but from what I understand this won't work because of the line break (see solution 1 above in proxyfilter.php) and I'm not entirely familiar with regex.
  2. Using substr or another similar function to search for the unique <div> tag and then just replace everything inside the tags rather than trying to use regex and wildcards (see solution 2 above in proxyfilter.php). I believe this solution is failing because it's just searching a predefined string where as I am searching within an HTML document and I'm not entirely sure how to bridge the gap there, if that makes sense.

I know the proxyfilter.php itself is working because I am able to successfully replace static strings found in the html using str_replace. Any suggestions on how to accomplish this successfully would be greatly appreciated.

1
  • what is your desired out put from the input so that we can help you easly Commented May 26, 2016 at 6:03

1 Answer 1

0

I think you just need the s modifier.

See Including new lines in PHP preg_replace function

So your code would be

$html = preg_replace('/<div id="id1" class="class1" >(.*?)<\/div>/s', '<div id="id1" class="class1" >"new-string"</div>', $html);
Sign up to request clarification or add additional context in comments.

Comments

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.