Jump to content

Regex Help!!!


d_barszczak

Recommended Posts

Hi all,

 

I am useless when it comes to regex so all help is much appreciated.

 

I have php template system which i use and i am about to add a new utitity to it that easily allow you to add resources to a page. I need to be able to pick tags out of a page of html. e.g.

 

<p>[resource type=image' id='1' height='100' width='100]</p>

 

I would like the script to detect the [resource ] tags and any data within such as id='1' place in an array.

 

Is this even possible?

Link to comment
https://forums.phpfreaks.com/topic/112191-regex-help/
Share on other sites

Not gonna be easy, but still possible.

 

<?php
$string = "<p>[resource type='image' name='photo' id='1' height='100' width='100']</p>";

/* get [resource ...] */
preg_match("/(\[resource[^\]]+\])/", $string, $a);

/* get properties from [resource ...] */
preg_match_all("/([\w]+='[\w]+')/", $a[1], $b);

/* replace key='val' to key=val */
$c = preg_replace("/([\w]+)='([\w]+)'/", "$1=$2", $b[1]);

/* convert obtained string to associative array */
foreach($c as $line) {
list($key, $val) = explode('=', $line);
$props[$key] = $val;
}

print '<pre>';
print_r($props);

/* now you have:
$props = array
(
[type] => image
[name] => photo
[id] => 1
[height] => 100
[width] => 100
)
*/
?>

Link to comment
https://forums.phpfreaks.com/topic/112191-regex-help/#findComment-575952
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.