Page 1 of 1

Preg_match find the * in $value2

Posted: Mon Dec 28, 2009 5:10 am
by ibom
Do someone know how to find the * in $value2 by using the preg_match

Code: Select all

 
<?php
 
$value2 = 10 * 10;
$value = ($value2 - ($value2 + $value2)); // creates a negative value
 
if(!$value || $value > 0 || !is_numeric($value) || [color=#408000]preg_match("/*/", $value2)[/color]){
                     echo "Insert a positive number please";
                     }else { 
        echo $value;
               } // the value will be in negative
?>
 

Re: Preg_match find the * in $value2

Posted: Mon Dec 28, 2009 6:33 am
by requinix
That code is all manners of wacky.

Find what? Where?

Re: Preg_match find the * in $value2

Posted: Mon Dec 28, 2009 6:43 am
by Apollo
ibom wrote:$value2 = 10 * 10;
This is the same to PHP as

Code: Select all

$value2 = 100;
so there won't be any * characters in there at all Image

But I assume you need a preg_match to check if there is a * character in a string, well here ya go:

Code: Select all

if (preg_match("/\\*/", $s)) print("yep!")

Re: Preg_match find the * in $value

Posted: Mon Dec 28, 2009 7:12 am
by ibom
Thanks for your replay.. I run the code and it do not recognize the * in value. I did some retypeing in the code so it will be more simple..

Here it is as full code..
The preg_match should find the * in the value... Any suggestions...

Code: Select all

 
<?php
 
$value = -10 [color=#008000]*[/color] 2;
 
if(!$value || $value > 0 || !is_numeric($value) || [color=#008000]preg_match("/\\*/", $value)[/color]){
                     echo "Insert a positive number please";
                     }else { 
                        echo $value;
               }
?>
 

Re: Preg_match find the * in $value2

Posted: Mon Dec 28, 2009 8:24 am
by ibom
Any help...

Re: Preg_match find the * in $value2

Posted: Mon Dec 28, 2009 11:12 am
by manohoo
You reversed the < symbol. Try this:

Code: Select all

 <?php
 
$value = -10 * 2;
 
if(!$value || $value < 0 || !is_numeric($value) || preg_match("/\\*/", $value)){
  echo "Insert a positive number please";
}else {
  echo $value;
}
?> 

Re: Preg_match find the * in $value

Posted: Mon Dec 28, 2009 11:35 am
by Apollo
ibom wrote:The preg_match should find the * in the value... Any suggestions...
Again:
Apollo wrote:
ibom wrote:$value2 = 10 * 10;
This is the same to PHP as

Code: Select all

$value2 = 100;
so there won't be any * characters in there at all Image
If you still don't get the point, keep in mind that

Code: Select all

$value2 = 10 * 10;
is something different than

Code: Select all

$value2 = "10 * 10";
:!:

Re: Preg_match find the * in $value2

Posted: Tue Dec 29, 2009 2:51 am
by ibom
I think that you are missing something, because it dont work.. You can see it by save it and run it yourself. I think that the preg_match thinks that it should "Match 0 or more times" in the value.

The Characthers are below...


Special Characters
The following should be escaped if you are trying to match that character

\ ^ . $ | ( ) [ ]
* + ? { } ,

Special Character Definitions
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
More Special Character Stuff
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\c[ control char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E
Even More Special Characters
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\b Match a word boundary
\B Match a non-(word boundary)
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only where previous m//g left off (works only with /g)



Code: Select all

 
<?php
 
$value = -10 * 2;
 
if(!$value || $value > 0 || !is_numeric($value) || preg_match("/\\*/",$s, $value) ){
                     echo "Insert a positive number please";
                     }else { 
                        echo $value;
               }
?>
 
 

Re: Preg_match find the * in $value2

Posted: Tue Dec 29, 2009 3:01 am
by requinix
How many times do we have to say this?

There is no * in $value. At all. Anywhere.

Re: Preg_match find the * in $value2

Posted: Tue Dec 29, 2009 4:10 am
by Apollo
Or wait, I think I got it. You need to use this:

Code: Select all

preg_match("/.*/",$value)
Then I'm sure it will find your * character! :crazy:

Re: Preg_match find the * in $value2

Posted: Tue Dec 29, 2009 4:46 am
by ibom
I get the point.. But I want you to look at this type of code and here you can add * , + , - , and hack the php code. So I can make the preg_match or any othor type of ereg() find the * , + , -


Look at the value of redraw in the input type...

Code: Select all

 
<?php
include("config.php");
if(isset($_POST['submit']))
  {
      $storeid = $_POST['storeid'];
      $text = $_POST['text'];
     [color=#408000] $value = $_POST['value'];[/color]
      
if(!$value || $value > 0 || !is_numeric($value) || [color=#408000]preg_match("/\\*/",$s, $value)[/color] ){
                       
                     echo "The value have to be in negative (-)";
                     exit(); 
              }
 
         $result = mysql_query("INSERT INTO transholder (storeid, text, value )
                       VALUES ('$storeid','$text','$value')") or die(mysql_error());
                       
    }
 
 
else
  {
 
      ?>
      <br>
       
      <form method="post" action="<?php echo $_SERVER[PHP_SELF] ?>">
 
      <input name="storeid" type="hidden" size="15" maxlength="45" value="<?php echo $_SESSION['id']; ?>">
      <input name="text" type="hidden" value="Redraws">
      [color=#408000]Value of redraw.: <input name="value" size="12" value="<?php echo $value; ?>">[/color]
      <center><input type="submit"  value="Submit" name="submit"/></center>
      </form>
      <?
  }
 
?>
 

Re: Preg_match find the * in $value2

Posted: Tue Dec 29, 2009 5:15 am
by Apollo
I don't know what exactly it is that you're trying to accomplish here. But if you want to verify a user input to be a positive integer, you could use a regexp like this:

Code: Select all

$value = $_POST['value'];
echo preg_match("/^[0-9]+$/",$value) ? "Thanks, that suits my needs" : "Insert a positive number please";
(ignoring any theoretical debates whether you consider "0" to be a positive number, allow prefix zeroes, how to deal with >32bit or >64 bit integers, etc ;))

Re: Preg_match find the * in $value2

Posted: Tue Dec 29, 2009 5:53 am
by ibom
OK. Thanks now it works just fine....