0

How can I get a specific string within a string using PHP?

i tried to used preg_match, but the result was not what I wanted.

Example String:

Creator: Adobe InDesign CC (Macintosh) Producer: Adobe PDF Library 10.0.1 CreationDate: Fri Aug 8 10:37:26 2014 ModDate: Fri Aug 8 10:37:29 2014 Tagged: no Form: none Pages: 2 Encrypted: no Page size: 612.283 x 858.898 pts (rotated 0 degrees) MediaBox: 0.00 0.00 612.28 858.90 CropBox: 0.00 0.00 612.28 858.90 BleedBox: 0.00 0.00 612.28 858.90 TrimBox: 8.50 8.50 603.78 850.39 ArtBox: 0.00 0.00 612.28 858.90 File size: 28176860 bytes Optimized: no PDF version: 1.6

This is a result for pdfinfo test.pdf comandline:

What i want.. is to get the specific string like this:

MediaBox: 0.00 0.00 612.28 858.90 CropBox: 0.00 0.00 612.28 858.90 BleedBox: 0.00 0.00 612.28 858.90 TrimBox: 8.50 8.50 603.78 850.39 ArtBox: 0.00 0.00 612.28 858.90

And put it in an array list.. The result would be something like this:

[
'Mediabox'  => [0.00,0.00,612.28,858.90],
'CropBox'   => [0.00 0.00 612.28 858.90],
'BleedBox'  => [0.00 0.00 612.28 858.90],
'TrimBox'   => [8.50 8.50 603.78 850.39]
]

3 Answers 3

2

One way to do it, explode the string at spaces

$str = "MediaBox: 0.00 0.00 612.28 858.90 CropBox: 0.00 0.00 612.28 858.90 BleedBox: 0.00 0.00 612.28 858.90 TrimBox: 8.50 8.50 603.78 850.39 ArtBox: 0.00 0.00 612.28 858.90"; 
$array = array(); 

foreach (explode(" ",$str) as $value)
{
    if (!is_numeric($value))
        $box = substr($value, 0, -1);
    else
        $array[$box][] = $value;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This one is correct to. Much Simpler... thanks Lenny
2

I tested this code and it seems to produce your expected output:

$str = "Creator: Adobe InDesign CC (Macintosh) Producer: Adobe PDF Library 10.0.1 CreationDate: Fri Aug 8 10:37:26 2014 ModDate: Fri Aug 8 10:37:29 2014 Tagged: no Form: none Pages: 2 Encrypted: no Page size: 612.283 x 858.898 pts (rotated 0 degrees) MediaBox: 0.00 0.00 612.28 858.90 CropBox: 0.00 0.00 612.28 858.90 BleedBox: 0.00 0.00 612.28 858.90 TrimBox: 8.50 8.50 603.78 850.39 ArtBox: 0.00 0.00 612.28 858.90 File size: 28176860 bytes Optimized: no PDF version: 1.6";

$matches = array();
$count   = preg_match_all("/(MediaBox|CropBox|BleedBox|TrimBox):\s([0-9]+\.[0-9]+\s[0-9]+\.[0-9]+\s[0-9]+\.[0-9]+\s[0-9]+\.[0-9]+)/", $str, $matches, PREG_PATTERN_ORDER);

header("Content-Type: text/plain;charset=UTF-8");

array_shift($matches);

$str_keys   = $matches[0];
$str_values = $matches[1];
$result     = array();

for ($i = 0; $i < $count; ++$i) {
  $result[$str_keys[$i]] = explode(" ", $str_values[$i]);
}

echo json_encode($result, JSON_PRETTY_PRINT);

Output:

{
    "MediaBox": [
        "0.00",
        "0.00",
        "612.28",
        "858.90"
    ],
    "CropBox": [
        "0.00",
        "0.00",
        "612.28",
        "858.90"
    ],
    "BleedBox": [
        "0.00",
        "0.00",
        "612.28",
        "858.90"
    ],
    "TrimBox": [
        "8.50",
        "8.50",
        "603.78",
        "850.39"
    ]
}

Hope this helps.

Comments

0

Using the following regex, you will get two capturing groups for each match. The first one is the key in your associative array, the second one is the value.

Here is the regex to use in preg_match for example:

(MediaBox|CropBox|BleedBox|TrimBox):((?: (?:\d+(?:\.\d{1,2}))){3})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.