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