Jump to content

Needles in Haystack, no idea how to parse / break apart...


edwardtilbury

Recommended Posts

So should explode. Unless it changes too much

Although explode isn't picking out the numbers, it's picking out the content between the brackets. The regexp is explicitly picking out numbers wherever they appear in the haystack.

 

Both should achieve the same result given this haystack (although you might want to unshift the first element from the result array for the explode method).

It'd be interesting to see which was the faster of the two approaches

Link to comment
Share on other sites

<?php

$var = "This complete set include the following parts: (12345), (12346), (12347)";
$k = strlen($var);
$p = $i = 0;
$parts = array();
$inside = 0;
while ($p < $k)
{
    switch ($ch = $var[$p])
    {
        case '(':
            $inside = 1;
            break;
        case ')':
            $inside = 0;
            ++$i;
            break;
        default:
            if ($inside) $parts[$i] .= $ch;
    }
    ++$p;
}

echo '<pre>', print_r($parts, true), '</pre>';
?>

-->
Array
(
    [0] => 12345
    [1] => 12346
    [2] => 12347
)

Link to comment
Share on other sites

Here is the output of your solution:

Yes but he won't be using print_r, yours will get any numbers ? am i right, even ones not inside of parenthesis?

Array ( [0] => This complete set include the following parts: [1] => 12345, [2] => 12346, [3] => 12347 )

To solve it he can do

<?php
$var = "This complete set include the following parts: (12345), (12346), (12347)";
$var = explode("(",$var);
$arr = array(")",",");
$var = str_replace($arr,"",$var);
for($i = 1;$i < count($var);$i++)
{
echo $var[$i]."<br>";
}
?>

Link to comment
Share on other sites

Regular expression are slow slow slow.

Often said, less often true.

 

Testing the following two variants on a function last week:

function coordinateFromString($pCoordinateString = 'A1') {
    $column = rtrim($pCoordinateString, '0..9');
    $row = ltrim($pCoordinateString, 'A..Z');
    return array($column,$row);
}

 

function coordinateFromString($pCoordinateString = 'A1') {
    $column = '';
    $row = '';
    if (preg_match("/([$]?[A-Z]+)([$]?\d+)/", $pCoordinateString, $matches)) {
        list(, $column, $row) = $matches;
    }
    return array($column, $row);
}

The preg_match variant not only returned correct values in all cases (with inputs in the formats "IV123", "$IV123", "IV$123", "$IV$123") but also performed faster as well.

Link to comment
Share on other sites

I am obviously the winner here.

 

<?php
$var = "This complete set include the following parts: (12345), (12346), (12347)";
$n = 1;
$p = false;
for ($x = 0; $x < strlen($var); $x++) {
   if (is_numeric(substr($var,$x,1))) {
    $nums[$n] .= substr($var,$x,1);
	  $p = true;
   } else {
    $p = false;
      $n++;
 }
}
$nums = array_values($nums);
print_r($nums);
?>

Link to comment
Share on other sites

Yeah I spent a good 5 minutes trying to figure out what all the $p = boolean business was doing.  It's a very nice solution though.

 

Cleaned up a lil:

 

<?php
$var = "This complete set include the following parts: (12345), (12346), (12347)";
for ($x = 0; $x < strlen($var); $x++) {
if (is_numeric($var[$x])) {
	$nums[$n] .= $var[$x];
} else {
	$n++;
}
}
$nums = array_values($nums);
print_r($nums);
?>

Link to comment
Share on other sites

I tried the solutions, but then realized that I present the wrong haystack! ::)DOH! So I cannot pick a winner yet.

 

 

 

You guys are too helpful! ;D

 

Ok, I made a mistake, I checked the database, and actually it's a little more complicated... ???

 

The variable has numbers and text, also the needles are not separated uniformly.

 

 

 

Please solve for this new haystack...

 

Haystack 2.0

This 19th century easter egg basket includes the following parts: A giant robot from the year 2008 (12345), Left window trim for a 1993-2001 Lexus SC400 (12346), and a 1/6th scale papier-mache head of Ron Paul made from native indians in Luxembourg(12347)

 

 

Link to comment
Share on other sites

@flyhoney: Anyways, dunno about it being a "very nice" solution... I personally would opt for the regex route. I was just being silly.

 

And on that note...

I tried the solutions, but then realized that I present the wrong haystack! ::)DOH! So I cannot pick a winner yet.

 

 

 

You guys are too helpful! ;D

 

Ok, I made a mistake, I checked the database, and actually it's a little more complicated... ???

 

The variable has numbers and text, also the needles are not separated uniformly.

 

Haystack 2.0

This 19th century easter egg basket includes the following parts: A giant robot from the year 2008 (12345), Left window trim for a 1993-2001 Lexus SC400 (12346), and a 1/6th scale papier-mache head of Ron Paul made from native indians in Luxembourg

 

 

Use the regex solution just alter it to only grab numbers between parenthesis.

Link to comment
Share on other sites

Like this:

 

<?php
$numbers = array();
$subject = 'This 19th century easter egg basket includes the following parts: A giant robot from the year 2008 (12345), Left window trim for a 1993-2001 Lexus SC400 (12346), and a 1/6th scale papier-mache head of Ron Paul made from native indians in Luxembourg';
$pattern = '/\(([\d]+)\)/';
preg_match_all($pattern, $subject, $numbers);
$numbers = array_pop($numbers);
print_r($numbers);
?>

 

I was being facetious :P.  Either way, it is a creative solution.

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.