0

Hi,

I am trying to search for numbers in an array - "explodedArray" which has been "exploded from a text entry. The aim is to be able to allow the user to type something in like " 5 + 5 " or "5 plus 5" or even "5 plus 5 minus 10 + 4" etc...

The following code was a test to implement the "5 plus 5" scenario. Unfortunately, it doesn't work. I am just beginning with PHP and have looked through a lot of things and this is what I wrote.

If you could help me, show me another method or something along those lines, I would be grateful.

Thanks very much.

if (array_search("plus", $explodedArray)) 

		
			{
				
				$explodedArrayNumber = count($explodedArray); 
				
				while ($explodedArrayNumber > 0)
					
					{
						$explodedArrayNumber -= 1;
						
							if (is_int($explodedArray[$explodedArrayNumber]))
							
								{
									$total += $explodedArray[$explodedArrayNumber];
									echo "$total";  
								}
						
					}
			}

Sorry for the weird indenting - it looks very different ( a lot better) in Dreamweaver but really bad here.

Edited by tusharbhatia: n/a

2
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by tusharbhatia
Featured Replies
  • 1
    diafol 3,669   5 Years Ago

    You need to search for integers, floats, brackets, arithmetic operators (-+/* etc), I believe you can search for numbers integers or floats thus with preg_match_all() [B]/(\d+\.*\d*)/[/B] You can check for operators: [B]/(\-|\+|\/|\*)/[/B] Use the the [B]PREG_OFFSET_CAPTURE[/B] flag with the two above to get the position of the numbers and operators. … Read More

0

hmm, I think you'd be better off storing numbers and operators with something like preg_ functions?

Otherwise, you're going to use eval() - which is very rarely a good idea

0

Hmm... I looked over the internet and saw a couple of examples of those. How would I implement it? I'm not even sure if I understand what they are properly.

0

Also, for some reason I'm getting an error here when I load the page. After I search, it works great. What's going wrong there? I know its to do with there being nothing in the textbox when you start the page. However, I can't think of a good condition for the if statement.

<form action="AnswerPage.php" method="get"> 
	<input type="search" name="search" size="80%"/> 
        <input type="submit" style="display:none"/>
</form> 

<?php
$Question = $_REQUEST['search']; 
echo "$Question" ;
?>
0

You're looking for $_REQUEST on first page load, even though the form hasn't been sent. Two things - use $_GET if using method="get" and $_POST if using method="post" - don't use $_REQUEST.
Also it's usually better to have form handling code in a different page, i.e. the action attribute should be to a different page. You just redirect back if required later on.

0

Ok, thanks. That problem is sorted. Sorry, I was referring to the Preg functions you were talking about.

Edited by tusharbhatia: n/a

0

Sorry, Sorry! I meant the problem of the $_GET / $_POST / $_REQUEST.

My issue is still not solved. I've come up with the code below and it doesn't seem to be working.

My problem is that I want to find numbers in the array and add them together.

<?php
session_start(); 

//search for the relavent numbers in the array.
$_SESSION['numbersArray'] = array("0");

if ($_SESSION['Question'] != "" || $_SESSION['Question'] != " ") 

{ 
	//loop through array
	
	foreach ($_SESSION['explodedQ'] as $x) 
	{
			
			//finds each numeric. 
			if (is_numeric($x)) 
		
			{
				array_push($_SESSION['numbersArray'], $x);
				print_r($_SESSION['numbersArray']); 
			}
			
	}
}

Once again, sorry for being confusing.

1

You need to search for integers, floats, brackets, arithmetic operators (-+/* etc),

I believe you can search for numbers integers or floats thus with preg_match_all()

/(\d+\.*\d*)/

You can check for operators:

/(\-|\+|\/|\*)/


Use the the PREG_OFFSET_CAPTURE flag with the two above to get the position of the numbers and operators.

Then you can check the validity of each array item with is_numeric for numbers and with a simple if statement for operators.

Combine the arrays, sort it on the 'position value' and implode the array to spit out the cleaned sum.
You *should* now be able to use eval() safely, ensuring that it does not run any horrible php code.

This is just a thought - I've never done this myself.

Edited by diafol: n/a

0

Thanks, I will give that a go. I looked at some more documentation as well as your post which helped.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.