1

I am new to programming and I'm trying to build a little php price comparison script for personal use. I allready managed to parse a site of a webshop (using simple dom parser), and get a (sort of) cleaned up string with a tier and a price in it.

The strings I am working with are now formated like this:

" 50  27,00 "  //50 pieces of a product cost €27,00 (without the ""s)
"1000  26,60 " //1000 pieces of a product cost €26,60

I want to grab the first part of the string to $tier, and the second part (including the comma) to the string $price.

Can you help me how to do this? Sometimes the spaces to begin the string with vary (see example above. There are always 2 white spaces in the middle.

An array would be fine also if I could get it like this (whithout the spaces):

$pricearray = array(50, "27,00"); //string to number will be my next problem to solve, first things first 

I think I have to use preg_split, but don't now the expression to use.

Thank you for thinking with me.

Wiz
  • 59
  • 1
  • 1
  • 2
  • http://stackoverflow.com/questions/1063087/how-to-split-a-string-with-php – Frank van Puffelen Dec 24 '12 at 20:56
  • Don't fall into the trap of thinking you have to use a regular expression for every programming problem that involves strings. There are often much simpler ways to achieve common tasks, such as breaking apart a string on whitespace. I suggest reading through [all of the built-in PHP string functions](http://php.net/manual/en/ref.strings.php) and getting an idea of what is available to you. – Andy Lester Dec 24 '12 at 22:02

2 Answers2

4

The simplest way is to call explode function:

$string = '1000 26,60';
$pricearray = explode(' ', $string);

But first of all, you have to get rid of all unnecessary spaces:

$string = trim($string); // remove spaces at the beginning and at the end
$string = preg_replace('/\s+/', ' ', $string); // replace 1+ spaces with 1 space

The space replacement method was taken from this question. Thanks, codaddict!

Community
  • 1
  • 1
akashivskyy
  • 44,342
  • 16
  • 106
  • 116
  • Thank you Kashiv. It works perfect! The only thing I can't get working is the preg_replace. It keeps working with 2 spaces and thus with one space in the array. – Wiz Dec 24 '12 at 21:19
1

Well, regex engines are little hard to understand but they can do those optional spaces easily.

Let see if I did not make mistake in the regex pattern:

$yourarray = array();
//just extract the pattern you want
preg_match( '/([0-9]+) + ([0-9]+,[0-9]+)/', " 50  27,00 ", $yourarray );
var_dump( $yourarray );
preg_match( '/([0-9]+) + ([0-9]+,[0-9]+)/', "1000  26,60 ", $yourarray );
var_dump( $yourarray );

// validate and extract the pattern you want
if ( !preg_match_all( '/^ *([0-9]+) +([0-9]+,[0-9]+) *$/', " 50  27,00 ", $yourarray ) )
  print "error";
else
  var_dump( $yourarray );
if ( !preg_match_all( '/^ *([0-9]+) + ([0-9]+,[0-9]+) *$/', "1000  26,60 ", $yourarray ) )
  print "error";
else
  var_dump( $yourarray );
if ( !preg_match_all( '/^ *([0-9]+) + ([0-9]+,[0-9]+) *$/', "1000 26 ", $yourarray ) )
  print "error";
else
  var_dump( $yourarray );
cavila
  • 7,834
  • 5
  • 21
  • 19