1

How can I detect and replace a soundcloud url in a text with an iframe using PHP:

For example:

This:

https://soundcloud.com/s/eminem-ft-dr-dre-old-time-sake

Into this:

<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/140068709&amp;color=00aabb&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe>

2 Answers 2

0
  function linkifySoundcloudURLs( $text )
  {
    $text = preg_replace('#https{0,1}:\/\/w{0,3}\.*soundcloud\.com\/([^< ]+)#ix',
    '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https://soundcloud.com/$1&amp;color=00aabb&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe>',
      $text);
    return $text;
  }

Works for me.

0

I'm not sure I understand you correctly, but something like this?

<?php
  $url = "https://soundcloud.com/aathmigan/eminem-ft-dr-dre-old-time-sake";
?>    

<iframe width="100%" height="166" scrolling="no" frameborder="no"
    src="<?php echo $url; ?>"></iframe>
4
  • I'm not sure why you would like to use preg_replace. Why not set a url-variable using if, else if and else instead? Help me understand, explain what it is for and how it should be used.
    – SebHallin
    Mar 7, 2015 at 18:36
  • I need a function with preg replace because it should first detect the link in the text and then replace the link
    – brox
    Mar 7, 2015 at 19:05
  • If-else would be much easier, but if you really need preg_replace, use this link.
    – SebHallin
    Mar 8, 2015 at 16:51
  • This link is great if you want to play with regular expressions.
    – SebHallin
    Apr 1, 2015 at 18:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.