I am trying to extract URL of Facebook / Twitter / Pinterest from shortcode which I put in my blog content using PHP's Regular Expression . The shortcode takes the following form like,
[embed type="facebook"]https://www.facebook.com/Techzoom.TV/posts/1960179340843049[/embed]
[embed type="Twitter"]https://twitter.com/HelloBdWorld/status/1568289461284524033[/embed]
I am not familiar with RegEx. But, I played with it a little and came with the pattern, like:
preg_match('/(\[embed\stype\=\"[facebook] \"\]).*(\[\/embed\])/i', '[embed type="facebook"]https://www.facebook.com/Techzoom.TV/posts/1960179340843049[/embed]', $socialOccurrences);
The output that I get, is:
Array ( [0] => [embed type="facebook"]https://www.facebook.com/Techzoom.TV/posts/1960179340843049[/embed] [1] => [embed type="facebook"] [2] => [/embed] )
But, it's not giving me the URL (https://www.facebook.com/Techzoom.TV/posts/1960179340843049).
Can anyone help me solve the problem?
CodePudding user response:
Going through your example:
If you want an URL you should capture it inside the group.
The
[facebook]
means "a single character in the list facebok between 1 and unlimited times". If you want to match the whole word - you need to wrap in in group as well.
So your fixed solution might be like this:
/\[embed type="(?:facebook|twitter|pinterest)"\](.*)\[\/embed\]/ig
This way you declare 2 groups - the website name (facebook
, Twitter
, ...) and the URL. Since you need only the URL, I assumed the former group might be ignored.