jnerotrix Posted December 7, 2008 Share Posted December 7, 2008 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 https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/ Share on other sites More sharing options...
.josh Posted December 7, 2008 Share Posted December 7, 2008 [^\d] Link to comment https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708625 Share on other sites More sharing options...
jnerotrix Posted December 7, 2008 Author Share Posted December 7, 2008 what do i do with [^\d] Link to comment https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708627 Share on other sites More sharing options...
.josh Posted December 7, 2008 Share Posted December 7, 2008 use [^\d] instead of [^A-Z] Link to comment https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708628 Share on other sites More sharing options...
jnerotrix Posted December 7, 2008 Author Share Posted December 7, 2008 Now it changes numbers to 0 zero also i dont want this Just Symbols = + ; " like this and letters A-Z But Numbers are Unchanged Link to comment https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708630 Share on other sites More sharing options...
jnerotrix Posted December 7, 2008 Author Share Posted December 7, 2008 I need so When user types in the form a letter or Symbol or any thing other than a number it replaces that letter or or symbol with the number 0 zero Link to comment https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708632 Share on other sites More sharing options...
jnerotrix Posted December 7, 2008 Author Share Posted December 7, 2008 I solved it i used this code $roll_no = $_POST['roll_no']; $string = $roll_no; $replace = "0"; $result = preg_replace("/[^0-9s]/","$replace", $string); $roll_no = $result; Link to comment https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708642 Share on other sites More sharing options...
.josh Posted December 7, 2008 Share Posted December 7, 2008 \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 https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708693 Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 \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 https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708697 Share on other sites More sharing options...
.josh Posted December 7, 2008 Share Posted December 7, 2008 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 https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708713 Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 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 https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708715 Share on other sites More sharing options...
.josh Posted December 7, 2008 Share Posted December 7, 2008 why don't you try putting an 's' in your $roll_no string $roll_no = "hello 12343 hello$%#% ssss"; Link to comment https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708718 Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 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 https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-708727 Share on other sites More sharing options...
effigy Posted December 8, 2008 Share Posted December 8, 2008 [^\d] FYI: You can use \D for this also. Link to comment https://forums.phpfreaks.com/topic/135942-solved-preg_replace-help/#findComment-709500 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.