NameSilo

[PHP] Extracting text

SpaceshipSpaceship
Watch

w1ww

Established Member
Impact
9
Hello,

Can you guys help me a little bit with this (PHP).

Imagine that I've this html code:

Code:
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="437" height="288" id="viddler_aae0f0d6"><param name="movie" value="http://www.viddler.com/player/aae0f0d6/" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><embed src="http://www.viddler.com/player/aae0f0d6/" width="437" height="288" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" name="viddler_aae0f0d6" ></embed></object>

I want to extract the id field id="viddler_aae0f0d6".

How can I do this knowing that every time the id it's going to be different?

So basically to extract "viddler_aae0f0d6" from this code.

Anyone has ideas?

Regards!
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
PHP:
$htmlcode='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="437" height="288" id="viddler_aae0f0d6"><param name="movie" value="http://www.viddler.com/player/aae0f0d6/" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><embed src="http://www.viddler.com/player/aae0f0d6/" width="437" height="288" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" name="viddler_aae0f0d6" ></embed></object>';
$theid = preg_replace('/(.*) id=\"(.*)\"(.*)/Usi','$2=_=',$htmlcode);
list($theid,$dump)=explode("=_=",$theid);

echo $theid;
$theid, contains the new id
 
0
•••
PHP:
$id = substr($text, strpos($text, ' id="') + 5, 16);

Assumes constant length id, (I checked other videos on the site, they all have a 16 id too, so not a problem.)

dan.
 
0
•••
I would use preg_match.

http://uk2.php.net/preg_match

Code:
<?php

$htmlcode='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="437" height="288" id="viddler_aae0f0d6"><param name="movie" value="http://www.viddler.com/player/aae0f0d6/" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><embed src="http://www.viddler.com/player/aae0f0d6/" width="437" height="288" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" name="viddler_aae0f0d6" ></embed></object>';
$count = preg_match('/\sid\s*=\s*"(.*?)"/',$htmlcode, $matches);
if ($count)
{
    $theid = $matches[1];
    echo $theid;
}
else
{
    echo "ID not found\n";
}

?>

Unlinke the previous solutions, this will also match with whitespaces before/after the equals sign or line breaks before the id tag. If you know what characters to expect in the id value, you could improve the regexp. Currently it will match any characters within the quotes.
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomainEasy — Zero Commission
DomDB
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back