5

サウンド クラウドから iframe が埋め込まれた wordpress データベースがあります。iframe をある種のショートコードに置き換えたい。ショートコードも作成しましたが、かなりうまく機能します。

問題は、既にコードが埋め込まれている約 2000 の投稿がある古いデータベースがあることです。私がやりたいのは、iframeをショートコードに置き換えるようにコードを書くことです。

これは、コンテンツからURLを見つけるために使用しているコードですが、常に空白を返します。

$string = 'Think Kavinsky meets Futurecop! meets your favorite 80s TV show theme song and you might be pretty close to Swedish producer Johan Bengtsson\'s retro project, <a href="https://soundcloud.com/daataa"><strong>Mitch Murder</strong></a>. Title track, "The Touch," is genuinely lighthearted and fun, crossing over from 80s synth work into a bit of French Touch influence; also including a big time guitar solo straight out of your dad\'s record collection. B-side "Race Day" could very easily be the soundtrack to a video montage of all of your favorite beach scenes from every 80s movie you\'ve ever watched, or as the PR put it, "quite possibly a contender to be the title screen music to a Wave Race 64 sequel." Sounds awesome to me. Also included in this package out today on <a href="https://soundcloud.com/maddecent/">Mad Decent</a>\'s Jeffree\'s sub-label are two remixes of the A-side from Lifelike and Nite Sprite. Download below.
<iframe src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F8087281&amp;color=000000&amp;auto_play=false&amp;show_artwork=true" frameborder="no" scrolling="no" width="100%" height="350"></iframe>';

preg_match("/url=(.*?)/", $string, $matches);

print_r($matches);

上記のコードは機能せず、私は正規表現にあまり詳しくないので、ここで何が問題なのかを誰かが理解できれば、それは素晴らしいことです. また、誰かがこれを行うための正しいプロセスを案内してくれるなら、それは素晴らしいことです.

4

5 に答える 5

4

ここでは HTML を扱っているので、DOM 関数を使用することをお勧めします。

$doc = new DOMDocument;
$doc->loadHTML($string);

foreach ($doc->getElementsByTagName('iframe') as $iframe) {
    $url = $iframe->getAttribute('src');
    // parse the query string
    parse_str(parse_url($url, PHP_URL_QUERY), $args);
    // save the modified attribute
    $iframe->setAttribute('src', $args['url']);
}

echo $doc->saveHTML();

これは完全なドキュメントを出力するため、それをトリミングする必要があります。

$body = $doc->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $node) {
    echo $doc->saveHTML($node);
}

出力:

<p>Think Kavinsky meets Futurecop! meets your favorite 80s TV show theme song and you might be pretty close to Swedish producer Johan Bengtsson's retro project, <a href="https://soundcloud.com/daataa"><strong>Mitch Murder</strong></a>. Title track, "The Touch," is genuinely lighthearted and fun, crossing over from 80s synth work into a bit of French Touch influence; also including a big time guitar solo straight out of your dad's record collection. B-side "Race Day" could very easily be the soundtrack to a video montage of all of your favorite beach scenes from every 80s movie you've ever watched, or as the PR put it, "quite possibly a contender to be the title screen music to a Wave Race 64 sequel." Sounds awesome to me. Also included in this package out today on <a href="https://soundcloud.com/maddecent/">Mad Decent</a>'s Jeffree's sub-label are two remixes of the A-side from Lifelike and Nite Sprite. Download below.
<iframe src="http://api.soundcloud.com/playlists/8087281" frameborder="no" scrolling="no" width="100%" height="350"></iframe></p>
于 2013-08-21T06:33:01.393 に答える
2

1 回限りの修正の場合は、SQL ソリューションを検討してください。次の SQL でのいくつかの仮定:

  • 投稿ごとに置き換える iframe は 1 つだけです (複数の iframe を含む投稿がある場合、SQL は複数回実行できます)。
  • ALL を置き換える iframe は次の形式です。

<iframe src="https://w.soundcloud.com/player/?url="..." other-stuff</iframe>

  • あなたが気にするのは、urlパラメータの引用符の間にあるものだけです
  • 最終結果は [soundcloud url="..."] です

これがすべて当てはまる場合、次の SQL でうまくいくはずです。別のショートコードなどが必要な場合は、微調整できます。

一括更新を実行する前に、必ず wp_posts テーブルをバックアップしてください。

CREATE TABLE wp_posts_backup SELECT * FROM wp_posts
;

バックアップが完了すると、次の SQL によってすべての投稿が一度に修正されます。

UPDATE wp_posts p

   SET p.post_content = CONCAT( SUBSTRING_INDEX( p.post_content, '<iframe src="https://w.soundcloud.com/player/?url=', 1 )
                               ,'[soundcloud url="'
                               , REPLACE( REPLACE(
                                 SUBSTRING_INDEX( SUBSTR( p.post_content
                                                        , LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
                                                        )
                                                , '&amp;', 1
                                                )
                               , '%3A', ':' ), '%2F', '/' )
                               ,'?'
                               ,SUBSTRING_INDEX( SUBSTR( p.post_content
                                                       , LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
                                                       + LOCATE( '&amp;', SUBSTR( p.post_content
                                                                                , LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
                                                                                )
                                                               ) + 4
                                                       )
                                               , ' ', 1
                                               )
                               ,']'
                               ,SUBSTR( p.post_content, LOCATE( '</iframe>', p.post_content ) + 9 )
                              )

 WHERE p.post_content LIKE '%<iframe src="https://w.soundcloud.com/player/?url=%</iframe>%'
;

すべての投稿に対してこれを実行する前に、いくつかの投稿をテストすることをお勧めします。テストする簡単な方法は、上記の WHERE 句 (';' の直前) に以下を追加して、'?' を変更することです。テストする投稿IDに。

AND p.ID IN (?,?,?)

何らかの理由で投稿を復元する必要がある場合は、次のようなことができます。

UPDATE wp_posts p
  JOIN wp_posts_backup b
    ON b.ID = p.ID
   SET p.post_content = b.post_content
;

考慮すべきもう1つのこと。現在 URL の一部になっているパラメーターを渡すかどうかわからなかったので、それらを含めました。次のように変更することで、それらを簡単に削除できます。

                               ,'?'
                               ,SUBSTRING_INDEX( SUBSTR( p.post_content
                                                       , LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
                                                       + LOCATE( '&amp;', SUBSTR( p.post_content
                                                                                , LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
                                                                                )
                                                               ) + 4
                                                       )
                                               , ' ', 1
                                               )
                               ,']'

に:

                           ,'"]'

その結果:

UPDATE wp_posts p

   SET p.post_content = CONCAT( SUBSTRING_INDEX( p.post_content, '<iframe src="https://w.soundcloud.com/player/?url=', 1 )
                               ,'[soundcloud url="'
                               , REPLACE( REPLACE(
                                 SUBSTRING_INDEX( SUBSTR( p.post_content
                                                        , LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
                                                        )
                                                , '&amp;', 1
                                                )
                               , '%3A', ':' ), '%2F', '/' )
                               ,'"]'
                               ,SUBSTR( p.post_content, LOCATE( '</iframe>', p.post_content ) + 9 )
                              )

 WHERE p.post_content LIKE '%<iframe src="https://w.soundcloud.com/player/?url=%</iframe>%'
;

URL にパラメータを許可しないように更新されました

UPDATE wp_posts p

   SET p.post_content = CONCAT( SUBSTRING_INDEX( p.post_content, '<iframe src="https://w.soundcloud.com/player/?url=', 1 )
                               ,'[soundcloud url="'
                               , REPLACE( REPLACE(
                                 SUBSTRING_INDEX(
                                     SUBSTRING_INDEX( SUBSTR( p.post_content
                                                            , LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
                                                            )
                                                    , '&amp;', 1
                                                    )
                                                , '"', 1
                                                )
                               , '%3A', ':' ), '%2F', '/' )
                               ,'"]'
                               ,SUBSTR( p.post_content, LOCATE( '</iframe>', p.post_content ) + 9 )
                              )

 WHERE p.post_content LIKE '%<iframe src="https://w.soundcloud.com/player/?url=%</iframe>%'
;

幸運を。

于 2013-08-24T22:20:37.277 に答える
1
<?php
    preg_match("/url\=([^\"]+)/i", $string, $matches);

したがって、基本的には、url= の後ではなく "

于 2013-08-13T12:40:20.430 に答える
1

simplehtmldom を調べることをお勧めします。jQueryやCSSに似たセレクターを利用したDOMパーサーです。

http://simplehtmldom.sourceforge.net/

$html = load($html_from_database);
// Find all frames
foreach($html->find('frame') as $element){
   $source = $element->src; // extract the source from the frame.
   // This is where you do your magic like changing links. 
   $element->href = $source ; // This is where you replace the old source
}


// UPDATE $html back into the table.

解析後にテーブルを更新する前に、すべてのテーブルの完全なバックアップを作成してください:)

http://simplehtmldom.sourceforge.net/manual.htm

于 2013-08-26T13:44:43.617 に答える