0

ユーザーが入力した値を抽出し、いくつかの高さと幅の値を置き換えてURLを保持するために、正規表現を作成しました。これは、データベースに安全に追加できるようにするためです。

これは私がこれまでに持っているものです(preg_matchがTRUE値を返すようにしようとしているだけです)

$test ='<object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false" type="application/x-shockwave-flash" width="100%"></embed> </object>'; 
  if (preg_match('/<object height=\"[0-9]*\" width=\"[0-9]*\"><param name=\"movie\" value=\"(.*)\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed allowscriptaccess=\"always\" height=\"[0-9]*\" src=\".*\" type=\"application\/x-shockwave-flash\" width=\"100%\"><\/embed><\/object>/', $test)) {

$embed = $test;

} else {

$embed = 'FALSE';

}

検証では常にfalseが返されるため、何か間違ったことをしたようです。

4

4 に答える 4

1

私が最初に失敗するのは次のとおりです。

width="100%"  will not match /width=\"[0-9]*\"/

正規表現の正確なPHP定義はわかりません。しかし、これが一致するかどうかはわかりません(reg-expressionのスペースは、ターゲットテキストの0個以上のスペースと一致する可能性がありますが、その逆は機能しません):

> <param      will not match (probably) /><param/

ご覧のとおり、正規表現を使用したXMLの解析は難しく、エラーが発生しやすくなっています。
本当にやりたいのは、XMLSAXパーサーを使用することです。

これを試してください:PS私のPHPは素晴らしいものではないので、間違いが含まれている可能性があります。

PS。長いURLはXML用に正しくエンコードされていませんでした。ここでは、エラーメッセージを停止するためにurlencode()を使用しました。私はそれが理にかなっているかどうかを確認しませんでした。

<?php

$test = '<object height="81" width="100%">'
            .'<param name="movie" value="'
                .urlencode('http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false')
            .'">'
            .'</param>'
            .'<param name="allowscriptaccess" value="always">'
            .'</param>'
            .'<embed allowscriptaccess="always" height="81" src="'
                .urlencode('http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false')
                .'" type="application/x-shockwave-flash" width="100%">'
            .'</embed>'
        .'</object>';

function JustPrint($parser,$data)
{
    print $data;
}

function OpenTag($parser,$name ,$attribs)
{
    // For special tags add a new attribute.
    if (strcasecmp($name, "object") == 0)
    {
        $attribs['Martin'] = 'York';
    }


    // Print the tag.
    print "<$name ";
    foreach ($attribs as $loop => $value)
    {
        print "$loop=\"$value\" ";
    }
    print ">\n";
}

function CloseTag($parser,$name)
{
    print "<$name/>\n";
}

$xmlParser  =  xml_parser_create();
xml_set_default_handler($xmlParser ,'JustPrint'  );
xml_set_element_handler($xmlParser, 'OpenTag'  , 'CloseTag'  );
xml_parse($xmlParser, $test);

?>
于 2010-08-17T05:58:14.557 に答える
1

This is what i use, replace $url by whatever var you are using:

if ( strtolower(str_ireplace('www.', '', parse_url($url, PHP_URL_HOST))) == 'soundcloud.com' ) { ?>
    <embed id="swf_u621112_1" width="890" height="84" flashvars="width=398&height=84" wmode="opaque" salign="tl" allowscriptaccess="never    " allowfullscreen="true" scale="scale" quality="high" bgcolor="#FFFFFF" name="swf_u621112_1" style="" src="http://player.soundcloud.com/p    layer.swf?url=<?php echo htmlspecialchars(urlencode($url)) ?>" type="application/x-shockwave-flash">
<?php
}
于 2011-04-08T21:33:10.003 に答える
0

可能であれば操作したくありません(高さの値を置き換えるだけです。そのままにしておきたいのですが、正規表現を使用してSQLインジェクションを最小限に抑え、埋め込みコードであることを確認しています。

文字列のように扱ってそのままにしておくだけでなく、何かと照合することはできますか?

たとえば、これはYouTubeの埋め込みリンクで機能します。

/preg_match(<object width=\"([0-9]*)\" height=\"([0-9]*)\"><param name=\"movie\" value=\"(.*)\"><\/param><param name=\"allowFullScreen\" value=\".*\"><\/param><param name=\"allowscriptaccess\" value=\".*\"><\/param><embed src=\".*\" type=\".*\" allowscriptaccess=\".*\" allowfullscreen=\".*\" width=\"[0-9]*\" height=\"[0-9]*\"><\/embed><\/object>/',$test,$preg_out)

preg_match [0] preg_match [1] preg_match [3]

オブジェクトの幅の高さとURLを返します。

于 2010-08-17T06:25:07.070 に答える
0

If what you want to do is allow the user to give you SoundCloud embeds while you retain the rights to style the players, you might want to look into oEmbed which is well supported by SoundCloud (see here) and other parties. This way, users just enter their normal track URLs and you can resolve those in the backend as you see fit.

Also, keep in mind that an embed code with a different order of <param>'s is still a valid embed code but would be very difficult to match with a regex

于 2010-08-23T15:01:47.893 に答える