Kommius Posted August 17, 2009 Share Posted August 17, 2009 Hi everyone, :mrgreen: I'm trying to code a relatively simple script. I want to first read the contents of the file, and then apply a stripslashes(); on all lines starting with the echo " tag and ending with "; . For now, here's what I was able to come up with : $file_contents = file_get_contents($fichier); if(preg_match_all('`^echo"(.*?)"', $file_contents, $matches)){ foreach ($matches as $val) { $processed .= stripslashes($val); } } else { $processed .= $file_contents; //-- What should i put here if the line doesn't correspond? } file_put_contents($fichier, $processed); Are my heading towards the right direction? Can anybody help me out? Thx Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/ Share on other sites More sharing options...
DarkendSoul Posted August 17, 2009 Share Posted August 17, 2009 This may work for you... <?php ob_start(); include($fichier); $output = stripslashes(ob_get_contents()); ob_end_clean(); echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-899903 Share on other sites More sharing options...
ignace Posted August 17, 2009 Share Posted August 17, 2009 What are you actually trying to accomplish? Do you want to read a php file, then search for all echo statements and add slashes to strings? You probably need to extend it as ' is also valid. Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-899921 Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 You can use preg_replace() with the e pattern modifier, treating the replacement as PHP. Or use preg_replace_callback(): <?php $file_contents = file_get_contents($fichier); $file_contents = preg_replace_callback( '~^echo\s*([\'"])(.*?)\1;~sm', create_function( '$matches', 'return stripslashes($matches[2]);' ), $file_contents ); ?> The pattern matches echo at the start of the string or at the start of a line (m modifier). Then zero or more whitespace characters followed by either a single or a double quote. Then anything including new lines (s modifier) are matched and captured, until the opening quote (\1) followed by a semi colon is found. But this will yield unexpected results with code like echo "testing with a $var" . 'the end'; Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-899927 Share on other sites More sharing options...
DarkendSoul Posted August 17, 2009 Share Posted August 17, 2009 thebadbad: I'm curious... could that problem be solves some how with something like... (([\'"])(.*?)\1.*)+ Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-899936 Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 In order for it to work flawlessly, we would have to write our own parser, taking every thing into account. And that's not easy, as you might have guessed. Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-899943 Share on other sites More sharing options...
Kommius Posted August 17, 2009 Author Share Posted August 17, 2009 What are you actually trying to accomplish? Do you want to read a php file, then search for all echo statements and add slashes to strings? You probably need to extend it as ' is also valid. The goal is to parse a file such as this : #================================================== # function HTTPReferer # Liste des référants #================================================== function HTTPReferer() { global $bgcolor1, $bgcolor2, $bgcolor3, $db, $admin_file; include_once("header.php"); NCGraphicAdmin(); OpenTable(); echo "<center><b>"._NCWHOLINKS."</b></center><br /><br />"; echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" bgcolor=\"$bgcolor2\">"; echo "<tr>"; echo "<td bgcolor=\"$bgcolor3\" align=\"center\"><b>"._NCHITS."<b></td>"; echo "<td bgcolor=\"$bgcolor3\" align=\"center\"><b>"._NCURL."</b></td>"; echo "<td bgcolor=\"$bgcolor3\" align=\"center\"><b>"._NCUPDATED."</b></td>"; echo "<td bgcolor=\"$bgcolor3\" align=\"center\"><b>"._NCDELETE."</b></td>"; echo "</tr>"; $req_refs = $db->sql_query("SELECT r_id, r_url, r_count, r_lasttime FROM ".TABLE_REFERER." ORDER BY r_count DESC"); $num_refs = $db->sql_numrows($req_refs); if (!empty($num_refs)) { while ($row = $db->sql_fetchrow($req_refs)) { $r_id = intval($row['r_id']); $r_url = filter($row['r_url'], nohtml); $r_url2 = urlencode($r_url); $r_count = intval($row['r_count']); $r_lasttime = formatTimestamp($row['r_lasttime']); echo "<tr><td bgcolor=\"$bgcolor1\" align=\"center\"><font class=\"content\">$r_count</font></td>"; echo "<td bgcolor=\"$bgcolor1\" width=\"100%\"><font class=\"content\"><a href=\"index.php?url=$r_url2\" target=\"_blank\">$r_url</a></font></td>"; echo "<td bgcolor=\"$bgcolor1\" nowrap> <font class=\"content\">$r_lasttime</font> </td>"; echo "<td bgcolor=\"$bgcolor1\" align=\"center\"><a href=\"".$admin_file.".php?op=DelReferer&ref_id=".$r_id."\"><img src=\"images/objets/wrong.gif\" border=\"0\" alt=\""._NCDELETE."\" title=\""._NCDELETE."\"></a></td>"; echo "</tr>"; } } else { echo "<tr><td align=\"center\" bgcolor=\"$bgcolor1\" colspan=\"4\">"._NCNOREFERERS."</td></tr>"; } echo "</table><br />"; echo "<form action=\"".$admin_file.".php\" method=\"post\">"; echo "<input type=\"hidden\" name=\"op\" value=\"DelReferer\">"; echo "<input type=\"hidden\" name=\"ref_id\" value=\"all\">"; echo "<center><input type=\"submit\" value=\""._NCDELETEALL."\"></center>"; CloseTable(); include_once("footer.php"); } The thing is, if I do a stripslashes on the entire file contents, the stripslashes function removes all backslashes, including the ones contained in the PHP Code. The script would also allow me to parse the file with a more complicated function, based on the htmLawed library. Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900016 Share on other sites More sharing options...
Kommius Posted August 17, 2009 Author Share Posted August 17, 2009 You can use preg_replace() with the e pattern modifier, treating the replacement as PHP. Or use preg_replace_callback(): <?php $file_contents = file_get_contents($fichier); $file_contents = preg_replace_callback( '~^echo\s*([\'"])(.*?)\1;~sm', create_function( '$matches', 'return stripslashes($matches[2]);' ), $file_contents ); ?> The pattern matches echo at the start of the string or at the start of a line (m modifier). Then zero or more whitespace characters followed by either a single or a double quote. Then anything including new lines (s modifier) are matched and captured, until the opening quote (\1) followed by a semi colon is found. But this will yield unexpected results with code like echo "testing with a $var" . 'the end'; Your function looks quite interesting, I will have a look at it and get back to you with more feedback.. As you pointed out, it may yield unexpected result. Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900023 Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 How do you want to parse the file? 'Cause if you remove the slashes you will obviously mess with the syntax. Do you want to grab the output of the script? Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900034 Share on other sites More sharing options...
Kommius Posted August 17, 2009 Author Share Posted August 17, 2009 How do you want to parse the file? 'Cause if you remove the slashes you will obviously mess with the syntax. Do you want to grab the output of the script? Removing the slashes allows the htmLawed library to read and modify it. The output of the script adds the backslashes again in the xhtml parts of the PHP file. Currently, the full script looks like this : $file_contents = file_get_contents($list); //-- Removing slashes if(get_magic_quotes_gpc()) { $text = stripslashes($file_contents); } $processed = htmLawed($text); file_put_contents($list, $processed); For now, the script manages to transform : #================================================== # function HTTPReferer # Liste des référants #================================================== function HTTPReferer() { global $bgcolor1, $bgcolor2, $bgcolor3, $db, $admin_file; include_once("header.php"); NCGraphicAdmin(); OpenTable(); echo "<center><b>"._NCWHOLINKS."</b></center><br /><br />"; echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" bgcolor=\"$bgcolor2\">"; echo "<tr>"; echo "<td bgcolor=\"$bgcolor3\" align=\"center\"><b>"._NCHITS."<b></td>"; echo "<td bgcolor=\"$bgcolor3\" align=\"center\"><b>"._NCURL."</b></td>"; echo "<td bgcolor=\"$bgcolor3\" align=\"center\"><b>"._NCUPDATED."</b></td>"; echo "<td bgcolor=\"$bgcolor3\" align=\"center\"><b>"._NCDELETE."</b></td>"; echo "</tr>"; $req_refs = $db->sql_query("SELECT r_id, r_url, r_count, r_lasttime FROM ".TABLE_REFERER." ORDER BY r_count DESC"); $num_refs = $db->sql_numrows($req_refs); if (!empty($num_refs)) { while ($row = $db->sql_fetchrow($req_refs)) { $r_id = intval($row['r_id']); $r_url = filter($row['r_url'], nohtml); $r_url2 = urlencode($r_url); $r_count = intval($row['r_count']); $r_lasttime = formatTimestamp($row['r_lasttime']); echo "<tr><td bgcolor=\"$bgcolor1\" align=\"center\"><font class=\"content\">$r_count</font></td>"; echo "<td bgcolor=\"$bgcolor1\" width=\"100%\"><font class=\"content\"><a href=\"index.php?url=$r_url2\" target=\"_blank\">$r_url</a></font></td>"; echo "<td bgcolor=\"$bgcolor1\" nowrap> <font class=\"content\">$r_lasttime</font> </td>"; echo "<td bgcolor=\"$bgcolor1\" align=\"center\"><a href=\"".$admin_file.".php?op=DelReferer&ref_id=".$r_id."\"><img src=\"images/objets/wrong.gif\" border=\"0\" alt=\""._NCDELETE."\" title=\""._NCDELETE."\"></a></td>"; echo "</tr>"; } } else { echo "<tr><td align=\"center\" bgcolor=\"$bgcolor1\" colspan=\"4\">"._NCNOREFERERS."</td></tr>"; } echo "</table><br />"; echo "<form action=\"".$admin_file.".php\" method=\"post\">"; echo "<input type=\"hidden\" name=\"op\" value=\"DelReferer\">"; echo "<input type=\"hidden\" name=\"ref_id\" value=\"all\">"; echo "<center><input type=\"submit\" value=\""._NCDELETEALL."\"></center>"; CloseTable(); include_once("footer.php"); } to #================================================== # function HTTPReferer # Liste des référants #================================================== function HTTPReferer() { global $bgcolor1, $bgcolor2, $bgcolor3, $db, $admin_file; include_once("header.php"); NCGraphicAdmin(); OpenTable(); echo "<center><b>"._NCWHOLINKS."<b><center><br /><br />"; echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" style=\"background-color: $bgcolor2;\">"; echo "<tr>"; echo "<td align=\"center\" style=\"background-color: $bgcolor3;\"><b>"._NCHITS."<b><td>"; echo "<td align=\"center\" style=\"background-color: $bgcolor3;\"><b>"._NCURL."<b><td>"; echo "<td align=\"center\" style=\"background-color: $bgcolor3;\"><b>"._NCUPDATED."<b><td>"; echo "<td align=\"center\" style=\"background-color: $bgcolor3;\"><b>"._NCDELETE."<b><td>"; echo "<tr>"; $req_refs = $db->sql_query("SELECT r_id, r_url, r_count, r_lasttime FROM ".TABLE_REFERER." ORDER BY r_count DESC"); $num_refs = $db->sql_numrows($req_refs); if (!empty($num_refs)) { while ($row = $db->sql_fetchrow($req_refs)) { $r_id = intval($row['r_id']); $r_url = filter($row['r_url'], nohtml); $r_url2 = urlencode($r_url); $r_count = intval($row['r_count']); $r_lasttime = formatTimestamp($row['r_lasttime']); echo "<tr><td align=\"center\" style=\"background-color: $bgcolor1;\"><font class=\"content\">$r_count<font><td>"; echo "<td style=\"background-color: $bgcolor1; width: 100%;\"><font class=\"content\"><a href=\"index.php?url=$r_url2\" target=\"_blank\">$r_url<a><font><td>"; echo "<td style=\"background-color: $bgcolor1; white-space: nowrap;\"><font class=\"content\">$r_lasttime<font><td>"; echo "<td align=\"center\" style=\"background-color: $bgcolor1;\"><a href=\"".$admin_file.".php?op=DelRefererref_id=\"><img src=\"images/objets/wrong.gif\" alt=\"\" title=\"\" style=\"border: 0px;\" /><a><td>"; echo "<tr>"; } } else { echo "<tr><td align=\"center\" colspan=\"4\" style=\"background-color: $bgcolor1;\">"._NCNOREFERERS."<td><tr>"; } echo "<table><br />"; echo "<form action=\"".$admin_file.".php\" method=\"post\">"; echo "<input type=\"hidden\" name=\"op\" value=\"DelReferer\" />"; echo "<input type=\"hidden\" name=\"ref_id\" value=\"all\" />"; echo "<center><input type=\"submit\" value=\"\" /><center>"; CloseTable(); include_once("footer.php"); } There are still problems though, as all encapsed values in double-double quotes return as empty values. Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900042 Share on other sites More sharing options...
Kommius Posted August 17, 2009 Author Share Posted August 17, 2009 You can use preg_replace() with the e pattern modifier, treating the replacement as PHP. Or use preg_replace_callback(): <?php $file_contents = file_get_contents($fichier); $file_contents = preg_replace_callback( '~^echo\s*([\'"])(.*?)\1;~sm', create_function( '$matches', 'return stripslashes($matches[2]);' ), $file_contents ); ?> The pattern matches echo at the start of the string or at the start of a line (m modifier). Then zero or more whitespace characters followed by either a single or a double quote. Then anything including new lines (s modifier) are matched and captured, until the opening quote (\1) followed by a semi colon is found. But this will yield unexpected results with code like echo "testing with a $var" . 'the end'; Your function looks quite interesting, I will have a look at it and get back to you with more feedback.. As you pointed out, it may yield unexpected result. I tried your function, it does not yield any results (any typo in there?) Even doing a : $test = preg_match_all('~^echo\s*([\"])(.*?)\1;~sm', $file_contents, $matches); does not match anything in a file containing these lines : echo "<select>"; echo "<td><tr>"; I'm looking into it.. Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900244 Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 That's because the caret (^) matches at the start of the string and at the start of a line. You have tabs in front of echo. You can simply remove the caret to make it match echo anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900250 Share on other sites More sharing options...
DarkendSoul Posted August 17, 2009 Share Posted August 17, 2009 Or make it include the possibility for tabs and spaces \t|\s (with word boundaries). '~^\b\t*|\s*\becho\s*([\'"])(.*?)\1;~sm' But then again if you have tabs and spaces it would be \t*\s* (no need for boundaries). Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900275 Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 In that case you could use \s* for any optional whitespace characters (including spaces, tabs and newlines). '~^\s*echo\s*([\'"])(.*?)\1;~sm' Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900296 Share on other sites More sharing options...
Kommius Posted August 17, 2009 Author Share Posted August 17, 2009 Thank you so much for your help and your quick replies, the script works like a charm.. I can't thank you enough for your patience and your help, this has helped me understand regular expressions and the usage of preg_replace_callback, which I find is complicated to understand with the PHP manual!! PS : Can somebody set this topic as solved please? Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900387 Share on other sites More sharing options...
ignace Posted August 17, 2009 Share Posted August 17, 2009 In order for it to work flawlessly, we would have to write our own parser, taking every thing into account. And that's not easy, as you might have guessed. All it takes is an implementation of the Interpreter pattern Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900392 Share on other sites More sharing options...
ignace Posted August 17, 2009 Share Posted August 17, 2009 Can somebody set this topic as solved please? You can set it as solved by clicking 'Solved' below left above 'Quick Reply' Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900400 Share on other sites More sharing options...
Kommius Posted August 17, 2009 Author Share Posted August 17, 2009 Can somebody set this topic as solved please? You can set it as solved by clicking 'Solved' below left above 'Quick Reply' Thx Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900411 Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 Thank you so much for your help and your quick replies, the script works like a charm.. I can't thank you enough for your patience and your help, this has helped me understand regular expressions and the usage of preg_replace_callback, which I find is complicated to understand with the PHP manual!! PS : Can somebody set this topic as solved please? You're welcome! Just remember that the script isn't very versatile, and won't convert every variant of echo blocks you throw at it. Happy coding Quote Link to comment https://forums.phpfreaks.com/topic/170619-solved-select-certain-lines-for-stripslashes/#findComment-900434 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.