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:
- The string I am trying to replace changes frequently.
- 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:
- Using
preg_replaceandregexto 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 inproxyfilter.php) and I'm not entirely familiar withregex. - Using
substror another similar function to search for the unique<div>tag and then just replace everything inside the tags rather than trying to useregexand wildcards (see solution 2 above inproxyfilter.php). I believe this solution is failing because it's just searching a predefined string where as I am searching within anHTMLdocument 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.