Solved

PHP RegEx extraction

Posted on 2013-11-11
7
288 Views
Last Modified: 2013-11-25
Given the following text snippet, I need some help in extracting the parameters:

$text = <<<EOT
[someothertag:no:thanks]
[mytag:charlie:doug]
[mytag:julie:ivy]
EOT;

I want to extract the fields from the mytag lines resulting in an array something like:

array(
 [0] => array('charlie', 'doug'),
 [1] => array('julie', 'ivy')
)

I thought preg_match might do this, but not had any joy.  Can anyone help?

Thanks
BT
0
Comment
Question by:brothertom
7 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 39638182
try this:

<?php
$text = <<<EOT
[someothertag:no:thanks]
[mytag:charlie:doug]
[mytag:julie:ivy]
EOT;

$one = explode("][", str_replace(PHP_EOL, "", $text));
print_r($one);
$arr = array();
foreach ($one as $s){
  $s = str_replace("[", "", $s);
  $s = str_replace("]", "", $s);
  $t = explode(":", $s);
  $arr[count($arr)] = array_slice($t, 1);
}
print_r($arr);

?>

Open in new window

HTH

Ivo Stoykov
0
 

Author Comment

by:brothertom
ID: 39638218
Ah, sorry I should have mentioned, the [..] tags can be anywhere in the text so won't often get ][ next to each other.

It's actually a snippet from a Wordpress page, so something like (nonsense example)

$text = <<<EOT
<h1>Page title</h1>
[someothertag:no:thanks]
<p>
[mytag:charlie:doug]
some more random text
[mytag:julie:ivy]
and some trailing text
EOT;
0
 
LVL 12

Accepted Solution

by:
zappafan2k2 earned 300 total points
ID: 39638556
preg_match_all() should work just fine.  You didn't show us what you've tried, so I can't comment on why it didn't work for you.
$text = <<<EOT
<h1>Page title</h1>
[someothertag:no:thanks]
<p>
[mytag:charlie:doug]
some more random text
[mytag:julie:ivy]
and some trailing text
EOT;

preg_match_all('/\[([^\]]+)\]/', $text, $matches);
print_r($matches);

Open in new window

yields
Array
(
    [0] => Array
        (
            [0] => [someothertag:no:thanks]
            [1] => [mytag:charlie:doug]
            [2] => [mytag:julie:ivy]
        )

    [1] => Array
        (
            [0] => someothertag:no:thanks
            [1] => mytag:charlie:doug
            [2] => mytag:julie:ivy
        )

)

Open in new window

So you will want to look at $matches[1].  From there, you can use preg_split() to pull the tags out.
$data = array();
foreach($matches[1] as $tags) {
    $tag = preg_split('/:/', $tags);  
    $mytag = array_shift($tag); // if you will always have mytag: first
    $data[] = $tag;
}
print_r($data);

Open in new window

yields
Array
(
    [0] => Array
        (
            [0] => no
            [1] => thanks
        )

    [1] => Array
        (
            [0] => charlie
            [1] => doug
        )

    [2] => Array
        (
            [0] => julie
            [1] => ivy
        )

)

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 39638653
As is true of almost every programming question ever asked, the quality and variety of responses is directly related to the quality and variety of the test data.

Please see http://www.laprbass.com/RAY_temp_brothertom.php

No regular expressions are needed at all.  It's simple string/array processing!

<?php // RAY_temp_brothertom.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28290580.html
$text = <<<EOT
<h1>Page title</h1>
[someothertag:no:thanks]
<p>
[mytag:charlie:doug]
some more random text
[mytag:julie:ivy]
and some trailing text
EOT;


// ISOLATE THE ELEMENTS BY USING THE TAG NAME
$arr = explode('mytag:', $text);
unset($arr[0]);

// ITERATE OVER THE ISOLATED ELEMENTS
foreach ($arr as $str)
{
    // KEEP ONLY THE PART TO THE LEFT OF THE CLOSING BRACKET
    $str = substr($str, 0, strpos($str, ']'));

    // PRODUCE THE ARRAY OF NAMES
    $out[] = explode(':', $str);
}

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($out);

Open in new window

Best regards, ~Ray
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 100 total points
ID: 39639042
Here you go:
$text = <<<EOT
<h1>Page title</h1>
[someothertag:no:thanks]
<p>
[mytag:charlie:doug]
some more random text
[mytag:julie:ivy]
and some trailing text
EOT;

preg_match_all('#\x5Bmytag:(?:((?:\x5C.|[^:])*):((?:\x5C.|[^\x5D])*))\x5D#', $text, $matches);
$matches=array_combine($matches[1],$matches[2]);

echo '<pre>',print_r($matches,true),'</pre>';
exit;

Open in new window

0
 

Author Closing Comment

by:brothertom
ID: 39676000
Thank you all - also, thanks Ray for a very interesting article on test data.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39676278
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now