doushao8421 2013-08-13 12:33
浏览 38
已采纳

preg_match查找并替换字符串模式

I have a wordpress database which has some embeded iframes from sound cloud. I want the the iframes to be replaced with some sort of shortcode. I have even created a shortcode and it works pretty good.

The problem is that i have an old database with approx 2000 posts which has already embeded codes. What i want to do is to write a code so that it would replace the iframe with the shortcode.

Here is the code which i am using to find the url from the content but it always returns blank.

$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);

The above code doesn't work and i am not so familiar with regex so if any one can figure out what is wrong here then it would be great. And also if anyone can guide me the right process to do this then that would be great.

  • 写回答

5条回答 默认 最新

  • dravpuso44681 2013-08-24 22:20
    关注

    For a one-time fix, you might consider an SQL solution. Some assumptions with the following SQL:

    • There is only ONE iframe per post to be replaced (the SQL can be run multiple times if there are posts with more than one iframe).
    • The iframes to be replaced ALL are in the form:

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

    • All you care about is what's between the quotes for the url parameter
    • The end result is [soundcloud url="..."]

    If all of this is true, then the following SQL should do the trick. It can be tweaked if you want a different shortcode, etc.

    Be sure to backup your wp_posts table before performing ANY mass update.

    CREATE TABLE wp_posts_backup SELECT * FROM wp_posts
    ;
    

    Once the backup is complete, the following SQL should fix all of your posts in a single shot:

    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>%'
    ;
    

    I would suggest you TEST a few posts before running this against all of them. An easy way to test would be to add the following to the WHERE clause above (immediately before ';') changing '?' to the Post ID(s) to be tested.

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

    If for any reason you need to restore your posts, you can do something like:

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

    One other thing to consider. I wasn't sure if you wanted to pass on the parameters that are currently a part of the url, so I included them. You can easily remove them by changing:

                                   ,'?'
                                   ,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
                                                   )
                                   ,']'
    

    to:

                               ,'"]'
    

    resulting in:

    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>%'
    ;
    

    Updated to allow for no parameters in the 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>%'
    ;
    

    Good luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP