Jump to content

[SOLVED] Just a little bit of string manipulation


optimaximal

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.