d_barszczak Posted June 27, 2008 Share Posted June 27, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/112191-regex-help/ Share on other sites More sharing options...
nashruddin Posted June 27, 2008 Share Posted June 27, 2008 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 ) */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/112191-regex-help/#findComment-575952 Share on other sites More sharing options...
d_barszczak Posted July 2, 2008 Author Share Posted July 2, 2008 Thanks!! That looks really complicated. Just wish i could get my head round it:) Quote Link to comment https://forums.phpfreaks.com/topic/112191-regex-help/#findComment-579938 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.