optimaximal Posted February 10, 2007 Share Posted February 10, 2007 Hi all, Can anyone give me a little nudge in the right direction about how I can take a string and replace all but the first 2 characters with asterisks? Im sure I should be using the strreplace function, as well as possibly left or something, but I'm having a brain fart. All help appreciated. Link to comment https://forums.phpfreaks.com/topic/37898-solved-just-a-little-bit-of-string-manipulation/ Share on other sites More sharing options...
GingerRobot Posted February 10, 2007 Share Posted February 10, 2007 If you definately want to show the number of characters in the string, rather than just having a fixed number of asterisks, then i think you'll need two functions, and do it something like: <?php $str = 'mystring'; $first_letters = substr($str, 0, 2); echo str_pad($first_letters, strlen($str), "*"); ?> If you were to go with just a fixed number of asterisks, you could use substr_replace on its own. Link to comment https://forums.phpfreaks.com/topic/37898-solved-just-a-little-bit-of-string-manipulation/#findComment-181413 Share on other sites More sharing options...
optimaximal Posted February 12, 2007 Author Share Posted February 12, 2007 Thanks for the help. Can someone debug this? Im sure it's pretty clear what i'm trying too do, although i'm getting the dreaded PHP white-screen-of-nothingness. I'm sure it's my syntax, but hey, any ideas welcome. Let me know if i've integrated the code right. <?php // Add what we need. require('connection.php'); include('header.php'); ?> </style></head> <body> <?php if (isset ($_POST['submit'])) { if ($_POST['audit_passsecure'] == '1') { $str = '{$_POST['audit_passpassword']}'; $first_letters = substr($str, 0, 2); $encrypt = str_pad($first_letters, strlen($str), "*"); } // Define the query. $query = "INSERT INTO audit_passwords (audit_passlocation,audit_passname,audit_passusername,audit_passpassword,audit_passdescr,audit_passsecure) VALUES ('{$_POST['audit_passlocation']}','{$_POST['audit_passname']}','{$_POST['audit_passusername']}', '$encrypt','{$_POST['audit_passdescr']}','{$_POST['audit_passsecure']}'); "; // Execute the query. if (@mysql_query ($query)) { ?> <p> Password Added to Database<br /> Click <a href="password_add.php">here</a> to add another.<br /> Click <a href="index.php">here</a> to return home. </p> <?php } else { print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>"; } mysql_close(); } else { ?> <form action="password_add.php" method="post"> <table width="100%" border="0"> <tr> <td width="200" valign="top">What is the Password For:</td> <td><input type="text" name="audit_passname" size="25" maxsize="6" /></td> </tr> <tr> <td width="200" valign="top">Password Location (i.e. Web Address)</td> <td><input type="text" name="audit_passlocation" size="25" maxsize="6" /></td> </tr> <tr> <td width="200" valign="top">Attached Username:</td> <td><input type="text" name="audit_passusername" size="25" maxsize="40" /></td> </tr> </tr> <tr> <td width="200" valign="top">Attached Password:</td> <td><input type="password" name="audit_passpassword" size="25" maxsize="40" /></td> </tr> <tr> <td width="200" valign="top">Additional Notes:</td> <td><textarea name="audit_passdescr" cols="40" rows="5" wrap="physical"></textarea></td> </tr> <tr> <td width="200" valign="top">Secure Password</td> <td><select name="audit_ltype"> <option value="0" selected>No</option> <option value="1">Yes</option> </select></td> </tr> </table> <br> <input type="submit" name="submit" value="Submit" /><input name="Reset" type="reset" value="Reset" /> </form> <?php } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/37898-solved-just-a-little-bit-of-string-manipulation/#findComment-182613 Share on other sites More sharing options...
ToonMariner Posted February 12, 2007 Share Posted February 12, 2007 change $str = '{$_POST['audit_passpassword']}'; to $str = $_POST['audit_passpassword']; Link to comment https://forums.phpfreaks.com/topic/37898-solved-just-a-little-bit-of-string-manipulation/#findComment-182630 Share on other sites More sharing options...
JJohnsenDK Posted February 12, 2007 Share Posted February 12, 2007 Try this. I removed all the {} also from your mysql_query. <?php if (isset ($_POST['submit'])) { if ($_POST['audit_passsecure'] == '1') { $str = '$_POST['audit_passpassword']'; $first_letters = substr($str, 0, 2); $encrypt = str_pad($first_letters, strlen($str), "*"); } // Define the query. $query = "INSERT INTO audit_passwords (audit_passlocation,audit_passname,audit_passusername,audit_passpassword,audit_passdescr,audit_passsecure) VALUES ('$_POST['audit_passlocation']','$_POST['audit_passname']','$_POST['audit_passusername']', '$encrypt','$_POST['audit_passdescr']','$_POST['audit_passsecure']'); "; // Execute the query. if (@mysql_query ($query)) { ?> <p> Password Added to Database<br /> Click <a href="password_add.php">here</a> to add another.<br /> Click <a href="index.php">here</a> to return home. </p> <?php } else { print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>"; } mysql_close(); } else { ?> Link to comment https://forums.phpfreaks.com/topic/37898-solved-just-a-little-bit-of-string-manipulation/#findComment-182648 Share on other sites More sharing options...
ToonMariner Posted February 12, 2007 Share Posted February 12, 2007 You may have to concat the $_POST vars in the query string - don't know why but I often get errors if I don't. Link to comment https://forums.phpfreaks.com/topic/37898-solved-just-a-little-bit-of-string-manipulation/#findComment-182655 Share on other sites More sharing options...
optimaximal Posted February 12, 2007 Author Share Posted February 12, 2007 ZING! ToonMariner, your solution worked. Link to comment https://forums.phpfreaks.com/topic/37898-solved-just-a-little-bit-of-string-manipulation/#findComment-182660 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.