nitromaster Posted August 27, 2008 Share Posted August 27, 2008 Currently learning php but got a problem I'd like to sort soon I have a scenario where I want a user to enter a widget code, submit the form, and then I have php which extracts the widgetid out of the code and then assigns it to a variable so I can use it elsewhere. I had originally thought of using preg_replace to replace all the html code before the id with nothing and doing the same with the html after the widgetid, as the base html would be the same except the id. Something like function removeHTML($widgetcode){ $find = array( '/htmlbeforewidgetidhere/', '/htmlafterwidgetid/' ); $replace = array( ' ', ' ' ); return preg_replace($find,$replace,$widgetcode); } $widget_id = RemoveHTML($widgetcode); However I've found out that the widget id is actually displayed twice so that wouldn't work, if anything i'd end up with the id twice or whatever. And that way was pretty hackish as well. So anyone got a better idea on what I should be using instead of preg_replace or what i should try with preg_replace? Here's an example widget code <div style="width:430px"><style>.mcrmeebo { display: block; background:url("http://widget.meebo.com/r.gif") no-repeat top right; } .mcrmeebo:hover { background:url("http://widget.meebo.com/ro.gif") no-repeat top right; } </style><object width="430" height="300" ><param name="movie" value="http://widget.meebo.com/mcr.swf?id=gdfryhdh"/><embed src="http://widget.meebo.com/mcr.swf?id=gdfryhdh" type="application/x-shockwave-flash" width="430" height="300" /></object><a href="http://www.meebo.com/rooms" class="mcrmeebo"><img alt="http://www.meebo.com/rooms" src="http://widget.meebo.com/b.gif" width="430" height="45" style="border:0px"/></a></div> I'd need to extract the id from the id= bit. Thanks Link to comment https://forums.phpfreaks.com/topic/121613-solved-how-to-parse-html-code-and-extract-a-certain-value/ Share on other sites More sharing options...
DarkWater Posted August 27, 2008 Share Posted August 27, 2008 <?php $widget = '<div style="width:430px"><style>.mcrmeebo { display: block; background:url("http://widget.meebo.com/r.gif") no-repeat top right; } .mcrmeebo:hover { background:url("http://widget.meebo.com/ro.gif") no-repeat top right; } </style><object width="430" height="300" ><param name="movie" value="http://widget.meebo.com/mcr.swf?id=gdfryhdh"/><embed src="http://widget.meebo.com/mcr.swf?id=gdfryhdh" type="application/x-shockwave-flash" width="430" height="300" /></object><a href="http://www.meebo.com/rooms" class="mcrmeebo"><img alt="http://www.meebo.com/rooms" src="http://widget.meebo.com/b.gif" width="430" height="45" style="border:0px"/></a></div>'; $matches = array(); preg_match('/mcr\.swf\?id=([a-z0-9]+)"/i', $widget, $matches); echo $matches[1]; ?> Link to comment https://forums.phpfreaks.com/topic/121613-solved-how-to-parse-html-code-and-extract-a-certain-value/#findComment-627312 Share on other sites More sharing options...
nitromaster Posted August 27, 2008 Author Share Posted August 27, 2008 Thank you Link to comment https://forums.phpfreaks.com/topic/121613-solved-how-to-parse-html-code-and-extract-a-certain-value/#findComment-627317 Share on other sites More sharing options...
DarkWater Posted August 27, 2008 Share Posted August 27, 2008 No problem. Link to comment https://forums.phpfreaks.com/topic/121613-solved-how-to-parse-html-code-and-extract-a-certain-value/#findComment-627319 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.