我使用preg_match_all
函数在PHP中创建了一个正则表达式,以在我的WordPress帖子中获取SoundCloud音频URL。正则表达式可以正常工作,没有任何问题:
preg_match_all[0][0] = https://soundcloud.com/anevo-remix/h3llo-anevo-remix-final
当我尝试
preg_match_all[0][0]
时,播放器不会加载,但是当我输入https://soundcloud.com/anevo-remix/h3llo-anevo-remix-final
网址时,播放器会加载而没有任何问题。如何使用PHP代码来加载播放器?<?php
$soundcloud_pattern = "/((http:\/\/(soundcloud\.com\/.*|soundcloud\.com\/.*\/.*|soundcloud\.com\/.*\/sets\/.*|soundcloud\.com\/groups\/.*|snd\.sc\/.*))|(https:\/\/(soundcloud\.com\/.*|soundcloud\.com\/.*\/.*|soundcloud\.com\/.*\/sets\/.*|soundcloud\.com\/groups\/.*)))/i";
if(preg_match_all($soundcloud_pattern, $subject, $soundcloud_audio_url)) {
var_dump($soundcloud_audio_url);
echo '
<script src="https://connect.soundcloud.com/sdk/sdk-3.0.0.js"></script>
<div id="soundcloud-audio"></div>
<script type="text/JavaScript">
SC.oEmbed("'.$soundcloud_audio_url[0][0].'", {
element: document.getElementById("soundcloud-audio")
});
</script>';
}
?>
最佳答案
尝试这样。我也没有理由使用preg_match_all
<?php
$soundcloud_pattern = "/((http:\/\/(soundcloud\.com\/.*|soundcloud\.com\/.*\/.*|soundcloud\.com\/.*\/sets\/.*|soundcloud\.com\/groups\/.*|snd\.sc\/.*))|(https:\/\/(soundcloud\.com\/.*|soundcloud\.com\/.*\/.*|soundcloud\.com\/.*\/sets\/.*|soundcloud\.com\/groups\/.*)))/i";
if (preg_match($soundcloud_pattern, $subject, $soundcloud_audio_url)) : ?>
<script src="https://connect.soundcloud.com/sdk/sdk-3.0.0.js"></script>
<div id="soundcloud-audio"></div>
<script>
SC.oEmbed(<?= json_encode(trim($soundcloud_audio_url[0])) ?>, {
element: document.getElementById("soundcloud-audio")
});
</script>
<?php endif ?>
关于javascript - 在Javascript代码中使用preg_match_all匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33643962/