views:

107

answers:

2

Hi everybody,

I am working with a CMS for a web app in PHP, that has the needs of shortening the process for inserting (embedding) stuff, like a video from youtube or vimeo by wroting the following, which are stored in the database:

<youtube id="wfI0Z6YJhL0" />

Which would output the following after some sort of replace:

<!-- Custom formatting before object !-->
<object width="640" height="385"><param name="movie" value="http://www.youtube-nocookie.com/v/wfI0Z6YJhL0&amp;amp;hl=sv_SE&amp;amp;fs=1?rel=0&amp;amp;color1=0xe1600f&amp;amp;color2=0xfebd01"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/wfI0Z6YJhL0&amp;amp;hl=sv_SE&amp;amp;fs=1?rel=0&amp;amp;color1=0xe1600f&amp;amp;color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
<!-- Custom formatting after object !-->

How could I do this in PHP?

+2  A: 

I'm not 100% sure how it will react to non-standard tags, but if it works, simpleHTMLDom will be perfect for this.

$html = str_get_html('....');

then something along the lines of ...

$element = $html->find('youtube',0 ); // Finds first element
                                      // - use "foreach" loop for final version 
$element->tag = 'object';
$element->value = "http://www.youtube.com/v/".$element-&gt;id;
$element->innertext= "<param ......>"

....

echo $html;

you get the drift.

The beauty of this approach would be that every specific extension could add its data in clean HTML notation <tagname attribute="value">, with even the possibility of adding sub-tags for structured info, instead of kludgy {placeholder}s and regexes and so on.

I have never tried this and I don't have the time to test this right now, but if you decide to give it a try, I'd be interested to know whether this way turned out to be useful.

Pekka
It should work just fine, since Simple DOM will parse XML and not just HTML, inspite of its' name. I've done a simple content replacement processing engine in the way you describe, although I used span:s with specific classes, specifying params in containing html comments.
nikc
it does work I did a comparison and it works fine, however it uses slightly more resources than Custom Tags, presumably because custom tags is aimed specifically at providing a specific level of functionality, so unless you need it I would go with CustomTags
buggedcom
+6  A: 

I've written a class that does exactly what you ask for my own cms. I've uploaded the src for you as although I've never released it the source is released under a BSD style license. Custom Tags

It basically allows you do do exactly what you ask for. In the class there are some example custom tags so I won't paste code here. Let me know how you go.

Edit 1: Example Code as requested. :-)

Edit 2: I should add it supports buried custom tags.

Edit 3: It also supports inline templating and tag substitution, ie

<ct:inline some="attribute">
    This is an in line template. <br />
    This is a #{tag} that can be accessed by the callback function
</ct:inline>

PHP/HTML: example.php

<?php

$current_dir = dirname(__FILE__).DIRECTORY_SEPARATOR;
require_once dirname($current_dir).DIRECTORY_SEPARATOR.'customtags.php';

$ct = new CustomTags(array(
    'parse_on_shutdown'     => true,
    'tag_directory'         => $current_dir.'tags'.DIRECTORY_SEPARATOR,
    'sniff_for_buried_tags' => true
));

?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd"&gt;

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>untitled</title>
    <meta name="generator" content="TextMate http://macromates.com/"&gt;
    <meta name="author" content="Oliver Lillie">
    <!-- Date: 2010-07-10 -->
</head>
<body> 

    <ct:youtube id="wfI0Z6YJhL0" />

</body>
</html>

Custom Tag PHP Function: tags/youtube/tag.php:

function ct_youtube($tag)
{
    return '<object id="'.$tag['attributes']->id.'" value="http://www.youtube.com/v/'.$tag['attributes']-&gt;id.'" /><param ......>';
}

Output:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd"&gt; 

<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>untitled</title> 
    <meta name="generator" content="TextMate http://macromates.com/"&gt; 
    <meta name="author" content="Oliver Lillie"> 
    <!-- Date: 2010-07-10 --> 
</head> 
<body> 

    <object id="wfI0Z6YJhL0" value="http://www.youtube.com/v/wfI0Z6YJhL0" /><param ......> 

</body> 
</html>
buggedcom
@buggedcom this sounds very interesting. Editing example code in here would be interesting for a quick comparison for future askers.
Pekka
I've just tested an example script comparing simpleHTMLDom against CustomTags and the comparison is so: simpleHTMLDom -> Time: 0.0057680606842 seconds, Mem Usage: 0.608268737793 MB, Mem Usage Peak: 0.654273986816 MB. CustomTags: Time: 0.00264501571655 seconds, Mem Usage: 0.452098846436 MB, Mem Peak Usage: 0.518165588379 MB. Turns out Custom Tags is slightly better performance wise so I would use that unless you need other specifics simpleHTMLDom uses.
buggedcom
+1 very nice! --
Pekka
I've been considering making a proper release of this but have not had the time. It does have great potential for providing cross cms functionality, ie video tags, swf tags etc etc. One day I'll get around to doing it...
buggedcom
Fantastic! Very nice!
Industrial
I'm having a [very similar problem](http://stackoverflow.com/questions/3944091/parse-cfml-tags-in-php) and this looks like it might just be the solution I've been looking for. Now if I could have just found in in July when you posted it...
Snekse