Panchoa
Panchoa

Reputation: 147

How to explode a string without proper delimiter - PHP

I'm doing a custom page for my WP website and for this I'm getting the content of an existing WP page. Getting the content is not a problem but I'm getting it like this :

https://youtu.be/abcdefghijkhttps://youtu.be/kjihgfedcbahttps://youtu.be/abcdefghijk

I'd like to transform it like this :

https://youtu.be/abcdefghijk
https://youtu.be/kjihgfedcba
https://youtu.be/abcdefghijk

I tried explode('/', $content) but it's not working as I want. I don't know if I can use substr() in this case.

How can I do to separate each url properly or atleast separate each video's id ?

Upvotes: 1

Views: 59

Answers (1)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

You can get all video id(s) as an array like this:

$str = "https://youtu.be/abcdefghijkhttps://youtu.be/kjihgfedcbahttps://youtu.be/abcdefghijk";
$videos = explode("https://youtu.be/",$str);

If you print_r($videos); you will see all the id in an array.

If you want these array values as full URLs, then do like this:

$videos = array_map(function($value) { return ' '.$value; }, $videos);

Upvotes: 4

Related Questions