Nir*_*Ram 5 php preg-replace html-parsing preg-match
我有一个wordpress数据库,它有一些嵌入来自声音云的iframe.我希望用某种短代码替换iframe.我甚至创建了一个短代码,它的效果非常好.
问题是我有一个旧的数据库,大约有2000个帖子,已经嵌入了代码.我想要做的是编写一个代码,以便用短代码替换iframe.
这是我用来从内容中找到网址的代码,但它总是返回空白.
$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&color=000000&auto_play=false&show_artwork=true" frameborder="no" scrolling="no" width="100%" height="350"></iframe>';
preg_match("/url=(.*?)/", $string, $matches);
print_r($matches);
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,我不熟悉正则表达式,所以如果有人能够弄清楚这里有什么问题那么它会很棒.而且,如果有人能指导我做正确的过程,那就太棒了.
对于一次性修复,您可以考虑使用 SQL 解决方案。以下 SQL 的一些假设:
<iframe src="https://w.soundcloud.com/player/?url="..." other-stuff</iframe>
如果所有这些都是正确的,那么下面的 SQL 应该可以解决问题。如果您想要不同的短代码等,可以对其进行调整。
在执行任何批量更新之前,请务必备份您的 wp_posts 表。
CREATE TABLE wp_posts_backup SELECT * FROM wp_posts
;
Run Code Online (Sandbox Code Playgroud)
备份完成后,以下 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
)
, '&', 1
)
, '%3A', ':' ), '%2F', '/' )
,'?'
,SUBSTRING_INDEX( SUBSTR( p.post_content
, LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
+ LOCATE( '&', 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>%'
;
Run Code Online (Sandbox Code Playgroud)
我建议您在针对所有帖子运行此操作之前测试一些帖子。一种简单的测试方法是将以下内容添加到上面的 WHERE 子句中(紧接在“;”之前)更改“?” 到要测试的帖子 ID。
AND p.ID IN (?,?,?)
Run Code Online (Sandbox Code Playgroud)
如果出于任何原因您需要恢复您的帖子,您可以执行以下操作:
UPDATE wp_posts p
JOIN wp_posts_backup b
ON b.ID = p.ID
SET p.post_content = b.post_content
;
Run Code Online (Sandbox Code Playgroud)
还有一件事需要考虑。我不确定您是否想传递当前属于 url 一部分的参数,因此我将它们包含在内。您可以通过更改以下内容轻松删除它们:
,'?'
,SUBSTRING_INDEX( SUBSTR( p.post_content
, LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
+ LOCATE( '&', SUBSTR( p.post_content
, LOCATE( '<iframe src="https://w.soundcloud.com/player/?url=', p.post_content ) + 50
)
) + 4
)
, ' ', 1
)
,']'
Run Code Online (Sandbox Code Playgroud)
到:
,'"]'
Run Code Online (Sandbox Code Playgroud)
导致:
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
)
, '&', 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>%'
;
Run Code Online (Sandbox Code Playgroud)
更新为允许 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
)
, '&', 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>%'
;
Run Code Online (Sandbox Code Playgroud)
祝你好运。