2

I've got the following data in a db

[508] [blah-blah-blah] Random Text
[510] [hello-hello-hello] More random text
[542] [stuff-stuff-stuff] Even more text

This is in quite a lot of db cells. The whole block of text is in a cell and each row of the text is separated by a carriage return.

Ideally i want the number in the first square bracket on each line into array values. The data i want to end up with is:

array(508,510,542)

More importantly, i want to see how to efficiently get that first data structure into a array. i feel like there should be a simple efficient way to use it, but, aside from some really complex regex i cannot see how to do it :(

Any help would be magical!

2
  • Which db are you using? Commented Jun 13, 2013 at 23:35
  • Mysql is the database :) Commented Jun 13, 2013 at 23:37

3 Answers 3

3

In PHP you can match the numbers using lookarounds for the literal brackets using preg_match_all()

<?php

    $string = '[508] [blah-blah-blah] Random Text
    [510] [hello-hello-hello] More random text
    [542] [stuff-stuff-stuff] Even more text';
    preg_match_all ('!(?<=\[)([0-9]+)(?=\])!',$string,$matches);
    print_r($matches[0]);

?>

Output

Array
(
    [0] => 508
    [1] => 510
    [2] => 542
)

To handle all of the records from the database, you would do something like this:

    $result = mysqli_query($sql);
$records = array(); 
while ($row = mysqli_fetch_array($result)){


        preg_match_all ('!(?<=\[)([0-9]+)(?=\])!',$row['my_text_field'],$matches);
        foreach($matches[0] as $value){     
        $records[]=$value;
        }

}

print_r($records);      
Sign up to request clarification or add additional context in comments.

1 Comment

This would fail if there are more than one column that starts with a number on any given line.
1

Given that your numbers always appear at the start of each line, the expression is pretty simple:

$input = <<<EOM
[508] [blah-blah-blah] Random Text
[510] [hello-hello-hello] More random text
[542] [stuff-stuff-stuff] Even more text
EOM;

preg_match_all('/^\[(\d+)\]/m', $input, $matches);
print_r($matches[1]);

I'm using the /m modifier to enable multiline mode, causing ^ to match the start of each line.

Comments

0

From the DB directly:

mysqli_query("SELECT SUBSTRING_INDEX(SUBSTRING(your_column_name, 1), ']', 1) FROM tablename");

SUBSTRING(str, pos) works like substr() would in php in that it extract a slice of the original string.

SUBSTRING(your_column_name, 1) means copy "your_column_name" starting from index 1 to the end.

[508] [blah-blah-blah] Random Text
Becomes
508] [blah-blah-blah] Random Text

SUBSTRING_INDEX(str, C, X) copy the entire string str until the Xth occurence of character C

SUBSTRING_INDEX(str, ']', 1) means copy the the entire string until the first occurence of ]

508] [blah-blah-blah] Random Text

Becomes

508

3 Comments

@AbsoluteƵERØ: Indeed, I misunderstood and thought one line in the exemple was one row in the database. That is one weird data set ...
It's cool. I'm not one for -1ing a potential answer. Definitely is a messed up entry, but stuff like this happens out there all the time. My favorite phrase is "We have a file in Word"...
Even if you could find a way to find all matches, you would still have to turn this into multiple rows to be effective.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.