Jump to content

[SOLVED] How to parse html code and extract a certain value?


nitromaster

Recommended Posts

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

<?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];
?>

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.