Jump to content

newb preg_match problem


completeNewb

Recommended Posts

Hi, preg_match isn't working the way I feel it should.

 

The idea, when i finally get this script working, is to echo an order form back to the client (so they can see what they've ordered). At the moment I'm just trying to get the product description for each ordered product.

 

$aryOrder:

prodCode[6722] with a value of (for instance) 2

prodCode[6724] with a value of (for instance) 1

etc

(only ordered products present in the array)

 

prodDB.txt

6722 Hydrocolloid Blister Care

6724 Toe Spreader

etc

All products present in text file (prode code Description)

 

BUT I get an echo with no matches for any ordered product (or any prod for that matter) i.e.

no match: 6724, 6724 TOE SPREADER

when clearly, the product code (6724 $aryItem) is present in the text file line ($line_of_text).

 

I just don't get it!

 

foreach($aryOrder as $aryItem => $qty) {

  $file_handle = fopen("prodDB.txt", "r");

  while (!feof($file_handle)) {

  $line_of_text = fgets($file_handle);

  if (preg_match($aryItem, $line_of_text))

    {

      echo "Matched! $aryItem, $line_of_text QTY: $qty <br>";

    }

  else

    {

  echo "no match: $aryItem, $line_of_text <br>";

    }

    }

  fclose($file_handle);

      }

 

I'm completely new to PHP, trying to work it out from the manual, so I've probably made a very basic mistake, and may even be going about this in entirely the wrong way.

Any help much appreciated,

Steve

Link to comment
Share on other sites

Instead of preg_match() try this:

 

if (strpos($line_of_text, $aryItem) !== false)

 

preg_match() needs a delimited string as input, eg "|6274|", where "|" is the delimiter.  preg_match() is much more powerful and flexible than strpos(), but from what I understand you don't need it here.

Link to comment
Share on other sites

Can you provide some code?

 

For one, I suggest using the file() function instead, which returns an array with each entry being a line in your file.

 

You shouldn't need to use RegEx either.

 

<?php 

//$products = file(prodDB.txt);

// This is just an example of what $products would look like
// so I don't have to create the file
$products = array(
'6722 Hydrocolloid Blister Care',
'6724 Toe Spreader',
'6726 Something',
'6728 SomethingElse',
'6730 FooBar'
);

$orders = array(
// prodID => quantity
'6722' => 2,
'6726' => 1,
'6730' => 4,
'9999' => 3
);

// Loop through orders
foreach( $orders as $item => $quantity ) {
if( ($key = array_search($item,$products)) !== FALSE ) {
	echo 'Matched! You want '.$quantity.' of '.$products[$key];
} else {
	echo 'Could not match '.$item;
}
echo '<br>';
}

?>

Link to comment
Share on other sites

This solution seems like it would be faster, and probably scale better than using a looped array_search() on a huge array.

 

<?php 

//$products_raw = file(prodDB.txt);

// This is just an example of what $products_raw would look like
// so I don't have to create the file
$products_raw = array(
'6722 Hydrocolloid Blister Care',
'6724 Toe Spreader',
'6726 Something',
'6728 SomethingElse',
'6730 FooBar',
'1234ThisWillError'
);
// Initialize our formatted array
$products = array();
// Loop through products and create an organized array
// This assumes it's formatted like ProdNumber{SPACE}Product Name
foreach( $products_raw as $line => $product ) {
// Make sure there's a space in the line, and grab it's position
if( ($split = strpos($product,' ')) === FALSE ) {
	// No space found, report it in some way
	echo 'Error trying to parse line '.($line+1).' ('.$product.')<br>';
	// And skip the rest of the code for this line
	continue;
}
// Split the string based on where the space is.
$id = substr($product,0,$split);
$name = substr($product,$split+1);
// Populate the formatted array
$products[$id] = $name;
}
// Destroy the raw array because we don't need it anymore
unset($products_raw);

$orders = array(
// prodID => quantity
'6722' => 2,
'6726' => 1,
'6730' => 4,
'9999' => 3
);

// Loop through orders
foreach( $orders as $item => $quantity ) {
// Now we don't have to use array_search, which is a slow function, especially when
// dealing with big arrays
if( isset($products[$item]) ) {
	echo 'Matched! You want '.$quantity.' of '.$products[$item];
} else {
	echo 'Could not match '.$item;
}
echo '<br>';
}

?>

 

Not benchmarked, so I could be wrong.

Link to comment
Share on other sites

Many thanks for your prompt replies.

 

btherl, I did try strPos early on but had no luck with it. Output was odd to say the least. I'm happy to try again if my latest efforts fail (I'm going to have to come to terms with it at some stage).

 

xyph, I've been trying your 1st suggested solution--which looked really straight-forward--but ended up with a blank return page every time. No idea why--I'm trying to do this by changing the sumbmission page and uploading it after every change, my server doesn't provide any feedback, either it works or it doesn't. I have yet to try with the constructed data (I went onto your 2nd suggested solution), but my problem may lie with $products = file(prodDB.txt); meaning that line wasn't working for me (and the products array wasn't being populated).

 

So I've now tried your 2nd suggested solution...

I added a line:

echo "line: $line , product: $product <br/>";

Hoping to see if prodDB.txt was indeed making it into an array.

I ordered just 2 products.

 

Got back the following:

 

Could not match 6722

Could not match 6724

 

So the code did execute, but I didn't get back the products array, which makes me suspect I'm doing something wrong with $products_raw = file(prodDB.txt);

 

Anyway, here's the code I used (can't seem to get the multicoloured code feature working):

 

$products_raw = file(prodDB.txt);
// Initialize our formatted array
$products = array();
// Loop through products and create an organized array
// This assumes it's formatted like ProdNumber{SPACE}Product Name
foreach( $products_raw as $line => $product ) {
echo "line: $line , product: $product <br/>";
// Make sure there's a space in the line, and grab it's position
if( ($split = strpos($product,' ')) === FALSE ) {
	// No space found, report it in some way
	echo 'Error trying to parse line '.($line+1).' ('.$product.')<br>';
	// And skip the rest of the code for this line
	continue;
}
// Split the string based on where the space is.
$id = substr($product,0,$split);
$name = substr($product,$split+1);
// Populate the formatted array
$products[$id] = $name;
}
// Destroy the raw array because we don't need it anymore
unset($products_raw);

// Loop through orders
foreach( $orders as $item => $quantity ) {
// Now we don't have to use array_search, which is a slow function, especially when
// dealing with big arrays
if( isset($products[$item]) ) {
	echo 'Matched! You want '.$quantity.' of '.$products[$item];
} else {
	echo 'Could not match '.$item;
}
echo '<br>';
}

 

Steve

 

 

 

Link to comment
Share on other sites

Hi, the above (last) script is failing at:

 

$products_raw = file(prodDB.txt);

 

The array $products_raw is not created.

 

If I use the code as supplied by xyph (with a locally constructed test array) the script works perfectly. (Thanks again xyph)

 

Any ideas why $products_raw = file(prodDB.txt); might fail?

The text file is in the same folder as the submit.php page.

 

Also this has gone off topic, what's the procedure? Start a new thread--i.e. "$newArray = file(textArray.txt) not working" or keep going here?

Steve

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.