So I have this little encryptor that I wrote, and I am having some
issues with the output on my last line. I would like it to echo '; so
I can just copy paste it straight across, instead it echo's out '.\r\n'

Issue is, since this is the last line, if I do preg_replace it might remove
all other concats and such. Oh the horror's, lol! There are a few other
things that I am toying with in here too, preg_replace <br> /w <br />
and etc, you can see them inside in the find and replace, none of those
really work, so you can ignore them, or .... ? PROFIT! ha, j/k

Anyway, I tried using preg_replace to match and replace the last line
with no luck. Anyone take a look and see if they have a better answer
to this? Thanks guys, here's the code, and a blank output to compare.

Blank output

'ppOyiN99rlsXV2QCdkOSMNt3xM16hx4k1hYuVK9DSJE='.
'

How blank output should look:

'ppOyiN99rlsXV2QCdkOSMNt3xM16hx4k1hYuVK9DSJE=';

Program:

<?php
    // Global defines
    define('VERSION', '1.2c');
    define('SALT', $salt);

$salt = 'MY.SALT.HERE';

function encrypt($text) {
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text,
    MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}

function decrypt($text) {
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text),
    MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}

 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>MCrypt v<?=VERSION ?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="author" content="J.d. (Jon Daniel)" />    
<meta name="copyright" content="http://ehime.deviantart.com" /> <meta name="Keywords" content=""/> <meta name="Description" content="Speed (de)MCrypt files" /> <style type="text/css"> /* <![CDATA[ */ body{color:#114;background-color:#333;font-family:Verdana;width:60%;} h2{color:#eee;padding:6px;font-size:16px;}div{width:98%;padding:6px;margin:6px;} form{height:360px;} .button {margin-top:-25px;} div.output{color:#114;background-color:#ccc;border:1pxdashed#fff;font-size:10px;} div.input{height:50%;background-color:#000;border:1pxdashed#0f0;} textarea.code{width:100%;height:300px;color:#0f0;background-color:#000;border:none;} input{background-color:#eee;color:#262;border:2pxsolid#ccc;font-weight:bold;}a.credits{color:#9f9;} div.credtis{color:#bbb;margin-top:50px;font-size:10px;} /* ]]> */ </style> <script type="text/javascript"> function clearCode() { var srcCode = document.getElementById('phpCode'); if (srcCode) { srcCode.value = ""; } } </script> </head> <body> <div class="output" style="background-color:black"> <?php switch ($_POST['crypt']) { case 'encrypt': $output = $_POST['phpCode']; $find = array( "°", "•", " < ", chr(10), chr(13), # Replace PHP /w Null "<?=$nwl;?>", '<?php include ($_SERVER[\'DOCUMENT_ROOT\'] . "/php/global2.php"); ?>', "<? $dir = 'images/products/laser/sub/';?>", ); $replace = array( "°", "•", " < ", "", "", # Replace PHP /w Null "<img alt='' class=\"nwl\" src=\"http://omnisistem.com/images/symbols/nwl.jpg\">", "", "", ); $output = str_replace($find, $replace, $output); $output = encrypt($output); $output = chunk_split($output, 85, "'.\r\n'"); $pattern = '//'; $replace = ''; # preg_replace($pattern, $replace, $output); echo "<br /><b style='color:red;'>Cipher:</b><br />"; echo "<textarea style='width:99.3%;background-color:#000;color:#0f0;'>'$output</textarea>"; echo "<br />"; $input = $_POST['phpCode']; $input = stripslashes($input); echo "<br /><b style='color:red;'>Rendered:</b>"; echo "<div style='color:#0f0; margin:-25px'>$input</div><br />"; break; case 'decrypt': $output = decrypt($_POST['phpCode']); $find = array( "<br>", ); $replace = array( "<br />", ); $output = str_replace($find, $replace, $output); $input = htmlspecialchars(stripslashes($output)); echo "<br /><b style='color:red;'>Source Code:</b><br />"; echo "<textarea style='width:99.3%;background-color:#000;color:#0f0;'>$input</textarea>"; echo "<br />"; $output = stripslashes($output); echo "<br /><b style='color:red;'>Rendered:</b>"; echo "<div style='color:#0f0; margin:-25px'>$output</div><br />"; break; } ?> </div> <div class="input"><form action="<?=$_SERVER['PHP_SELF'] ?>" method="post"> <div style='margin:5px 0;'> <input class="button" type="button" value="Clear Code" onclick="javascript:clearCode();" />&nbsp;&nbsp;&nbsp; <input class="button" type="submit" value="Execute Code" /><x style="margin:5%" /> <b style="color:#fff; border:1px dashed #0f0; padding:5px;"><input type="radio" name="crypt" value="encrypt" checked="checked" /> + <input type="radio" name="crypt" value="decrypt" /> - </b><x style="margin:5%" /><b style="color:#fff;margin-left:7px;"> <?="Salt: &nbsp;"?> <?="<textarea style='position:relative;top:4px;width:22%;height:18px;background-color:#000;color:#0f0;'>&nbsp;$salt</textarea>"?> </b> </div> <textarea id="phpCode" name="phpCode" class="code" rows="0" cols="0"><?php if (isset($_POST['phpCode'])) echo htmlentities(stripslashes($_POST['phpCode'])) ?></textarea><br /><br /> </form> </div> <div class="credtis"> <strong>MCrypt v<?=VERSION ?></strong>&nbsp; <a href="http://omnisistem.com/" target="_blank" class="credits">(About)</a><br /> ---<br /> Disclaimer:<br /> Use at your own risk. No warranty implied.<br /> <br /> &copy; 2oo9 - jd!<a href="http://omnisistem.com" target="_blank" class="credits">OmniSistem</a><br /> </div> </body> </html>

    Unless you enable multiline, there really is no problem at all with preg.

    $pattern = '#\.\s*\'#';
    // replace with ; and you're done

      Doesn't look like that worked? Mind creating the program in a file and see if you can
      get it to work?

        Won't let me edit my posts, here's the code I'm using ATM
        The line I user the preg_replace is 104-107 fyi

        <?php
            // Global defines
            define('VERSION', '1.2c');
            define('SALT', $salt);
        
        # var list
        $sp ='<x style="margin:5&#37;" />';
        $salt = 'o_0 Neko%$Mata 2051';
        
        function encrypt($text) {
            return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, 
        	MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
        }
        
        function decrypt($text) {
            return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), 
        	MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
        }
        
        /*==================================
        Replaces special characters with non-special equivalents
        ==================================*/
        function normalize( $str )
        {
            # Quotes cleanup
            $str = ereg_replace( chr(ord("`")), "'", 	$str );	# `
            $str = ereg_replace( chr(ord("&#180;")), "'", 	$str );	# &#180;
            $str = ereg_replace( chr(ord("&#8222;")), ",",	$str );	# &#8222;
            $str = ereg_replace( chr(ord("`")), "'",	$str );	# `
            $str = ereg_replace( chr(ord("&#180;")), "'", 	$str );	# &#180;
            $str = ereg_replace( chr(ord("&#8220;")), "\"", 	$str );	# &#8220;
            $str = ereg_replace( chr(ord("&#8221;")), "\"", 	$str );	# &#8221;
            $str = ereg_replace( chr(ord("&#180;")), "'", 	$str );	# &#180;
        
        $foreign_array = array(    
        	'&#352;'=>'S', '&#353;'=>'s', '&#381;'=>'Z', '&#382;'=>'z', '&#192;'=>'A', '&#193;'=>'A', '&#194;'=>'A', '&#195;'=>'A', '&#196;'=>'A', '&#197;'=>'A', '&#198;'=>'A', '&#199;'=>'C', '&#200;'=>'E', '&#201;'=>'E',
        	'&#202;'=>'E', '&#203;'=>'E', '&#204;'=>'I', '&#205;'=>'I', '&#206;'=>'I', '&#207;'=>'I', '&#209;'=>'N', '&#210;'=>'O', '&#211;'=>'O', '&#212;'=>'O', '&#213;'=>'O', '&#214;'=>'O', '&#216;'=>'O', '&#217;'=>'U',
        	'&#218;'=>'U', '&#219;'=>'U', '&#220;'=>'U', '&#221;'=>'Y', '&#222;'=>'B', '&#223;'=>'Ss', '&#224;'=>'a', '&#225;'=>'a', '&#226;'=>'a', '&#227;'=>'a', '&#228;'=>'a', '&#229;'=>'a', '&#230;'=>'a', '&#231;'=>'c',
        	'&#232;'=>'e', '&#233;'=>'e', '&#234;'=>'e', '&#235;'=>'e', '&#236;'=>'i', '&#237;'=>'i', '&#238;'=>'i', '&#239;'=>'i', '&#240;'=>'o', '&#241;'=>'n', '&#242;'=>'o', '&#243;'=>'o', '&#244;'=>'o', '&#245;'=>'o',
        	'&#246;'=>'o', '&#248;'=>'o', '&#249;'=>'u', '&#250;'=>'u', '&#251;'=>'u', '&#253;'=>'y', '&#253;'=>'y', '&#254;'=>'b', '&#255;'=>'y' );
        
        $str = strtr( $str, $foreign_array );
        
        # Bullets, dashes, and trademarks
        $str = ereg_replace( chr(149), "&#8226;", 	$str );		# bullet &#8226;
        $str = ereg_replace( chr(150), "&ndash;", 	$str );		# en dash
        $str = ereg_replace( chr(151), "&mdash;", 	$str );		# em dash
        $str = ereg_replace( chr(153), "&#8482;", 	$str );		# trademark
        $str = ereg_replace( chr(169), "&copy;", 	$str );		# copyright mark
        $str = ereg_replace( chr(174), "&reg;", 	$str );		# registration mark
        $str = ereg_replace( chr(176), "&#176;", 	$str );		# Degree
        
        return $str;
        }
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title>MCrypt v<?=VERSION ?></title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <meta name="author" content="J.d. (Jon Daniel)" />    
        <meta name="copyright" content="http://ehime.deviantart.com" /> <meta name="Keywords" content=""/> <meta name="Description" content="Speed (de)MCrypt files" /> <style type="text/css"> /* <![CDATA[ */ body{color:#114;background-color:#333;font-family:Verdana;width:60%;} h2{color:#eee;padding:6px;font-size:16px;}div{width:98%;padding:6px;margin:6px;} form{height:360px;} .button {margin-top:-25px;} div.output{color:#114;background-color:#ccc;border:1pxdashed#fff;font-size:10px;} div.input{height:50%;background-color:#000;border:1pxdashed#0f0;} textarea.code{width:100%;height:300px;color:#0f0;background-color:#000;border:none;} input{background-color:#eee;color:#262;border:2pxsolid#ccc;font-weight:bold;}a.credits{color:#9f9;} div.credtis{color:#bbb;margin-top:50px;font-size:10px;} /* ]]> */ </style> <script type="text/javascript"> function clearCode() { var srcCode = document.getElementById('phpCode'); if (srcCode) { srcCode.value = ""; } } </script> </head> <body> <div class="output" style="background-color:black"> <?php switch ($_POST['crypt']) { case 'encrypt': $output = $_POST['phpCode']; $output = normalize($output); $output = encrypt($output); $output = chunk_split($output, 85, "'.\r\n'"); $pattern = '//'; $replace = ''; preg_replace($pattern, $replace, $output); $pattern = '#\.\s*\'#'; $replace = ';'; preg_replace($pattern, $replace, $output); echo "<br /><b style='color:red;'>Cipher:</b><br />"; echo "<textarea style='width:99.3%;background-color:#000;color:#0f0;'>'$output</textarea>"; echo "<br />"; $input = $_POST['phpCode']; $input = stripslashes($input); echo "<br /><b style='color:red;'>Rendered:</b>"; echo "<div style='color:#0f0;'>$input</div><br />"; break; case 'decrypt': $output = decrypt($_POST['phpCode']); $find = array( "<br>", ); $replace = array( "<br />", ); $output = str_replace($find, $replace, $output); $input = htmlspecialchars(stripslashes($output)); echo "<br /><b style='color:red;'>Source Code:</b><br />"; echo "<textarea style='width:99.3%;background-color:#000;color:#0f0;'>$input</textarea>"; echo "<br />"; $output = stripslashes($output); echo "<br /><b style='color:red;'>Rendered:</b>"; echo "<div style='color:#0f0; margin-right:-25px'>$output</div><br />"; break; } ?> </div> <div class="input"><form action="<?=$_SERVER['PHP_SELF'] ?>" method="post"> <div style='margin:5px 0;'> <input class="button" type="button" value="Clear Code" onclick="javascript:clearCode();" /><?=$sp;?> <input class="button" type="submit" value="Execute Code" /><?=$sp;?> <b style="color:#fff; border:1px dashed #0f0; padding:5px;"><input type="radio" name="crypt" value="encrypt" checked="checked" /> + <input type="radio" name="crypt" value="decrypt" /> - </b><?=$sp;?><b style="color:#fff;margin-left:7px;"> <?="Salt: &nbsp;"?> <?="<textarea style='position:relative;top:4px;width:22%;height:18px;background-color:#000;color:#0f0;'>&nbsp;$salt</textarea>"?> </b> </div> <textarea id="phpCode" name="phpCode" class="code" rows="0" cols="0"><?php if (isset($_POST['phpCode'])) echo htmlentities(stripslashes($_POST['phpCode'])) ?></textarea><br /><br /> </form> </div> <div class="credtis"> <strong>MCrypt v<?=VERSION ?></strong>&nbsp; <a href="http://omnisistem.com/" target="_blank" class="credits">(About)</a><br /> ---<br /> Disclaimer:<br /> Use at your own risk. No warranty implied.<br /> <br /> &copy; 2oo9 - jd!<a href="http://omnisistem.com" target="_blank" class="credits">OmniSistem</a><br /> </div> </body> </html>

          First off, using ereg_replace when you should use str_replace is bad. Secondly, using ereg_anything is bad. Use preg.

          Have a look at the documentation for preg_replace. My pattern works as advertised.

          $pattern = '#\.\s*\'#';
          $replace = ';';
          $str = preg_replace($pattern, $replace, $output);	// Change your line to this
          echo $str . '<br/>';								// Add this to see the effect of my pattern
          

            Why is ereg bad to use? ill try the above and get back to you in a few minutes.

              ehime;10929862 wrote:

              Why is ereg bad to use? ill try the above and get back to you in a few minutes.

              what does it say in the big warning box on the manual page for ereg?

                ereg is part of POSIX (Portable Operating System Interface) that was meant as a standard in 1986 to be portable across different OSes. Turns out that Perl's regular expression system is superior. This is why we have Perl Compatible Regular Expressions (PCRE) as well in PHP.

                As of PHP 5.3, ereg (POSIX) is considered depreciated, and as of PHP 6, it will no longer be included within the core by default (I think it would have to be loaded via an extension or something like that - assuming of course your hosting provider even provides this once they do embrace / adpot version 6).

                So yeah, learn preg (which is part of PCRE) instead. You'll find it far more robust and useful.

                  Thanks guys, had no idea.

                  So I guess my big step for the day is migrating my ereg's to preg's eh?

                  There should be a function for that, lol =d

                    Write a Reply...