Jump to content

[SOLVED] preg_replace Help


jnerotrix

Recommended Posts

in my code i want it to replace Everything But Numbers to 0

 

So If a User Types Anything but a number it will replace it with 0

 

Heres the code i have so far

<?php
   $roll_no = $_POST['roll_no'];

$string = $roll_no;
$replace = "0";
$result = preg_replace("[^A-Z]","$replace", $string);
$roll_no = $result;
?>

Link to comment
Share on other sites

\d is shorthand for [0-9]  and why is that 's' in there? your [^0-9s] pattern will replace anything that is not a number or 's' with 0 so if you have this:

 

abcs123

 

you will get this:

 

000s123

 

 

[^\d] will replace anything that is not 0-9 just the same. If it didn't work then you messed up somewhere else in your code.

Link to comment
Share on other sites

\d is shorthand for [0-9]  and why is that 's' in there? your [^0-9s] pattern will replace anything that is not a number or 's' with 0 so if you have this:

 

abcs123

 

you will get this:

 

000s123

 

 

[^\d] will replace anything that is not 0-9 just the same. If it didn't work then you messed up somewhere else in your code.

 

I think that s represents the space if you do \ds the above function will work fine, but without the s it does not.

Link to comment
Share on other sites

s represents 's'

 

\s represents a space.

 

<?php
$patterns = array('[^\d]','[^0-9]','[^0-9s]','[^\ds]','[^0-9\s]','[^\d\s]');
$string = "aklsdj sldkj 23 0j0j23 0392sd0n iss230i ssss";
echo "<pre>";
echo "<table>";
echo "<tr><td>subject-></td><td>$string</td></tr>";
echo "<tr><td>pattern</td><td>result</td></tr>";
foreach ($patterns as $p) {
   echo "<tr><td>$p</td><td>" . preg_replace("/{$p}/", "0", $string). "</td></tr>";
}
echo "</table>";
echo "</pre>";
?>

 

output:

subject-> aklsdj sldkj 23 0j0j23 0392sd0n iss230i ssss
pattern   result
[^\d]     00000000000002300000230039200000000230000000
[^0-9]    00000000000002300000230039200000000230000000
[^0-9s]   000s000s0000023000002300392s00000ss23000ssss
[^\ds]    000s000s0000023000002300392s00000ss23000ssss
[^0-9\s]  000000 00000 23 000023 03920000 0002300 0000
[^\d\s]   000000 00000 23 000023 03920000 0002300 0000

Link to comment
Share on other sites

s represents 's'

 

\s represents a space.

 

<?php
$patterns = array('[^\d]','[^0-9]','[^0-9s]','[^\ds]','[^0-9\s]','[^\d\s]');
$string = "aklsdj sldkj 23 0j0j23 0392sd0n iss230i ssss";
echo "<pre>";
echo "<table>";
echo "<tr><td>subject-></td><td>$string</td></tr>";
echo "<tr><td>pattern</td><td>result</td></tr>";
foreach ($patterns as $p) {
   echo "<tr><td>$p</td><td>" . preg_replace("/{$p}/", "0", $string). "</td></tr>";
}
echo "</table>";
echo "</pre>";
?>

 

output:

subject-> aklsdj sldkj 23 0j0j23 0392sd0n iss230i ssss
pattern   result
[^\d]     00000000000002300000230039200000000230000000
[^0-9]    00000000000002300000230039200000000230000000
[^0-9s]   000s000s0000023000002300392s00000ss23000ssss
[^\ds]    000s000s0000023000002300392s00000ss23000ssss
[^0-9\s]  000000 00000 23 000023 03920000 0002300 0000
[^\d\s]   000000 00000 23 000023 03920000 0002300 0000

 

Hmm, not sure what is going on, but I get the same results as jnero when I run it with the s

 

<?php
   $roll_no = $_POST['roll_no'];

   $roll_no = "hello 12343 hello$%#%";
$string = $roll_no;
$replace = "0";
$result = preg_replace("/[^\ds]/",$replace, $string);
$roll_no = $result;
echo $result;
die;
?>

 

Result prints out

000000123430000000000

 

Maybe it is a server setting? I do not know...

Link to comment
Share on other sites

Very weird. I just tried it again with this:

 

<?php
   $roll_no = $_POST['roll_no'];

   $roll_no = "hello 12343 hello$%#%s";
$string = $roll_no;
$replace = "0";
$result = preg_replace("/[^\d]/",$replace, $string);
$roll_no = $result;
echo $result;
die;
?>

 

And it now yields what it was doing with s...before it would just display the whole string. I must have been doing a typo somewhere...

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.