Code Monkey home page Code Monkey logo

Comments (3)

mpratt avatar mpratt commented on July 19, 2024 2

Hi!
Oh, my bad I understood it the other way around.

The first thing that comes to mind is converting the oembed tag into the url inside of it. You can pass the output to Embera and process.

There is a setting inside the ProviderCollectionAdapter called url_extractor_regex that could be changed in order to support oembed tags but It would require more changes into the library itself, so I think preparing the text before passing it through Embera is your best choice.

$text = '<p>This is a some text</p>
<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed>
</figure>
<p>Other Video: https://www.youtube.com/watch?v=pILCn6VO_RU</p>';

$text = preg_replace('~<oembed url="(https?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_)))"></oembed>~is', '$1', $text);

$embera = new Embera();
echo $embera->autoEmbed($text);

Outputs:

<p>This is a some text</p>
<figure class="media">
    <iframe width="480" height="270" src="https://www.youtube.com/embed/NyEq7unEv5I?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</figure>
<p>Other Video: <iframe width="480" height="270" src="https://www.youtube.com/embed/pILCn6VO_RU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>

That should work if there is only one oembed element in your content.

However I think the better solution should be something like this

$text = '<p>This is a some text</p>
<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed>
</figure>

<figure class="media">
    <oembed url="https://unsupportedprovider.com/watch?v=NyEq7unEv5I"></oembed>
</figure>

<p>Other Video: https://www.youtube.com/watch?v=pILCn6VO_RU</p>

<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=62EB4JniuTc"></oembed>
</figure>';


if (preg_match_all('~<oembed url="(https?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_)))"></oembed>~is', $text, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $m) {
        $text = str_replace($m['0'], $m['1'], $text);
    }
}

$embera = new Embera();
$text = $embera->autoEmbed($text);

foreach ((array) $matches as $m) {
    $text = str_replace($m['1'], $m['0'], $text);
}

echo $text;

The last loop ensures that we dont loose content in case there is an oembed tag with an unsupported oembed provider.

I think this is the best way to handle this type of scenarios.

from embera.

mpratt avatar mpratt commented on July 19, 2024

Hi @charlesastwood

Sorry for the late response. You can use the ignore_tags directive.

use Embera\Embera;

$config = [
    'ignore_tags' => [  'pre', 'code', 'a', 'img', 'iframe', 'oembed'  ]
];

$embera = new Embera($config);
echo $embera->autoEmbed('<p>This is a some text</p>
<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed>
</figure>
<p>Above and below some video</p>');

Should output

<p>This is a some text</p><figure class="media"><oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed></figure><p>Above and below some video</p>

As you can see, the url remains intact.

Let me know if that is what you are searching for.

from embera.

charlesastwood avatar charlesastwood commented on July 19, 2024

HI @mpratt

I was hoping it would translate to:

<p>This is a some text</p>
<figure class="media"><iframe width="480" height="270" src="https://www.youtube.com/embed/NyEq7unEv5I?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></figure>
<p>Above and below some video</p>

So it translates the URL into the iFrame using the autoEmbed functionality

from embera.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.