2

I'm created a regex in PHP by using the preg_match_all function to get a SoundCloud audio url within my WordPress post. The regex works correctly without any problem:

preg_match_all[0][0] = https://soundcloud.com/anevo-remix/h3llo-anevo-remix-final

When I try the preg_match_all[0][0] the player doesn't load, but when I put the https://soundcloud.com/anevo-remix/h3llo-anevo-remix-final url, the player loads without any problem. How can I use the PHP code such that the player loads?

<?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>';
}
?>
4
  • What does the generated HTML look like (ie use "View page source")? Commented Nov 11, 2015 at 5:06
  • Kow i found the problem. when i put the php code cause new line break. but i want remove the new line break in my php code how can i do this? Commented Nov 11, 2015 at 5:14
  • "new line break" where? How was a line-break causing the problem? Commented Nov 11, 2015 at 5:14
  • when i use php code the first double quote ramain at this line SC.oEmbed("soundcloud url but the second double quote comes to this line "{element: document.getElementById("soundcloud-audio") i used url manually when i put url between the double quotes in the same line don't cause any problem but when the second double quote comes to new line don't load player Commented Nov 11, 2015 at 5:29

1 Answer 1

2

Try like this. I also see no reason to use 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 ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.