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.

Recommended Answers

All 10 Replies

Member Avatar for diafol

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

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.

Member Avatar for diafol

What are what?

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" ;
?>
Member Avatar for diafol

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.

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

Member Avatar for diafol

OK, please mark the thread as solved (link below the edit box).

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.

Member Avatar for diafol

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.