1

In my code I am trying to extract src value from JavaScript variable but I am literary confused because I have two src values JavaScript so I am using explode function but it doesn't extract the url properly. My main goal is to get only .mp4 url from it but I don't how to extract it. I have no problem to use regex because it simple and short but I don't know how to use for it.

Here is the my PHP Code

$b = "var myVideo=videojs('my_play');myVideo.src([{type:\"video/mp4\",src:\"https://example.com/dl/abc123/video.mp4\"}])";
$data = explode('type:"video/mp4",src:"', $b);
echo $data[1];

Echo show this value

https://example.com/dl/abc123/video.mp4"}])

Expected output:

https://example.com/dl/abc123/video.mp4
2
  • Can you share your expected output? Commented Sep 17, 2017 at 10:26
  • see my updated question Commented Sep 17, 2017 at 10:29

1 Answer 1

1

Here we are using preg_match to achieve desired output.

Regex: /src\s*:\s*"\K[^"]+

1. src\s*:\s*"\K this will match src:" and \K will reset current match. Optionally we are taking care of spaces with \s*

2. [^"]+ This will match all except "

Try this code snippet here

<?php
ini_set('display_errors', 1);
$b = "var myVideo=videojs('my_play');myVideo.src([{type:\"video/mp4\",src:\"https://example.com/dl/abc123/video.mp4\"}])";
preg_match('/src\s*:\s*"\K[^"]+/', $b,$matches);
print_r($matches[0]);
Sign up to request clarification or add additional context in comments.

1 Comment

@Rtra Glad to help you my friend.. :)

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.