How to use PHP to replace some special data?

PHP code book (32) , Linux CD Disk(8), Apache Skills (732)

If we want to let remove “()”, and the number inside (), at the same time remove the whitespace before “,”. We can use PHP to meet the demand:

<?php

$pattern = ‘[0-9]{1}’;  //remove number(s);

$replacement = “”;

$string = “your text here”;

$removenumber = eregi_replace($pattern,$replacement,$string);

$pattern = ‘\[\]’; //remove [];

$replacement = “,”;

$henry = eregi_replace($pattern,$replacement,$removenumber);

$pattern = ‘\ ,’;  //remove the whitespace before “,”;

$replacement = “,”;

$result = eregi_replace($pattern,$replacement,$henry);

echo $result;

?>

We can use more simple code to achive the results. I just want to let you know each step, then you can combine the codes yourself.

We can copy the following code and  save them in a file called show.php:

<center>
<form name=”login” method=”post” action=”show.php”>
<table width=”300″ border=”0″ cellpadding=”0″>
  <tr>
    <td><textarea name=”content” rows=”5″ cols=”20″></textarea></td>
  </tr>
  <tr>
    <td><input type=”submit” value=”Submit” /></td>
  </tr>
</table>
<?php
$pattern = ‘[0-9]{1}’;
$replacement = “”;
$string =  $_POST[content];
$removenumber = eregi_replace($pattern,$replacement,$string);

$pattern = ‘\[\]’;
$replacement = “,”;
$henry = eregi_replace($pattern,$replacement,$removenumber);

$pattern = ‘\ ,’;
$replacement = “,”;
$result = eregi_replace($pattern,$replacement,$henry);
echo $result;
?>
</form>
</center>