Reputation:
I am trying to extract a string from this string:
...width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F84915247&color=da1716&auto_play=false&show_artwork=true"...
All I want is the URL portion:
http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F84915247&
This is the regex I made:
$regex = "/^httpamp$/", but it does not work.
Upvotes: 1
Views: 1143
Reputation: 89547
this will work:
$regex = '~url=\K\S+?&~';
(the \K
feature keeps only characters after its position)
or with a lookbehind:
$regex = '~(?<=url=)\S+?&~';
Upvotes: 1
Reputation: 503
@anubhava solution is good, but I've tweaked it to improve it, and to support the new iframe HTML5 SoundCloud embed code.
\?url=([^"]+)
That will match the url but not the trailing quotation marks. This way a user could input a SoundCloud widget, then the code would strip out the widget leaving only url on the input.
Then when you render to screen (output from your db), a second script could be written to match a SoundCloud url beginning with http://api.soundcloud.com and wrap it in the widget.
Upvotes: 0
Reputation: 784998
It is not advisable to parse HTML text with a regex, better to use DOM parser.
However if you know what you're doing then use following regex:
~\?url=([^;]+)~
Your desired URL is available in matched group # 1.
Upvotes: 0
Reputation: 8293
You could use:
/url=([^ ]*)/
The URL would be in the 1st capturing group.
Upvotes: 1