3

My website, has 2 database tables. 1 of them have the posts_table and the other one have the videos.

At the moment i am getting the text images etc , normally from the post_table table. In my CMS when we add a video there is added a short code

[media id=487 width=660 height=440]

This shortcode automaticly get the link of a video from the vid_table where the id is the same as the shortcode.

So what i want is:

  • I need to do the same thing that the short code do, when a video is added on CMS the short code is showed in the post, i need to delete the shortcode and instead of it want to be played a video that has the link on the vid_table.

I have some problems with my english , so if you dont understand again please tell me.

Any kind of help will be great.

Thank you.

EDITED: So i want to replace the whole media tag with a flash player, that plays the url that belongs to the ID in the media tag

BUMP !! CAN HELP PLEASE ?

Meo
  • 241
  • 5
  • 11
  • 1
    I'm having a bit of trouble understanding what exactly is the question. You already posted an implementation for changing the media tag to a html element. Would you like to make a general parser for all tags matching the pattern `[object ...]` and render html elements based on the parameters of the tag? E.g `[link id=123]` would be replaced by `...` – kgilden Sep 03 '11 at 15:22
  • 2
    @Meo I think most people don't understand what you're after. for example, you mention a "table" without defining it. If it's an HTML table, you [shouldn't use regular expressions on the serialized HTML in the first place](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). Please add more information about the table and hyperlink you speak of. – phihag Sep 03 '11 at 15:23
  • @phihag By table I believe s/he means "database table" – NullUserException Sep 03 '11 at 15:58
  • yes i mean database table sorry, and as i said in the post there is only the id given, i can get the id but how can i get the link that is associated with that id in a database table – Meo Sep 03 '11 at 16:07
  • @Meo how are you communicating with your database in other parts of your application? – kgilden Sep 03 '11 at 16:11

2 Answers2

1

What do you want exactly?, you can get media id out of the text using

$text = 'some stuff [media id=468 width=660 height=440] more stuff';

preg_match("/media id=(.*) w/",$text, $results);
$result = $results[0];
$result = str_replace("media id=","",$result);

$result = str_replace("w","",$result);

$id = $result;
Ayush Pateria
  • 620
  • 12
  • 22
  • 1
    He wants to replace the whole media tag with a flash player, that plays the url that belongs to the ID in the media tag. – CodeCaster Sep 03 '11 at 17:46
1

This is quite a sophisticated problem actually. I was bored and made a basic tag parser. Right now it has some problems:

  • HTML rendering should be implemented in a separated class (and a template engine such as Twig should do the rendering);
  • Tag parsing is way too naive and will probably give you unexpected results if a tag's syntax is incorrect;
  • [media] tag does not support IE. You would have to change the source itself (method TagParser::renderMedia())

Some features to note:

  • extra parameters will be rendered as attributes for [link] tag, e.g [link id=25 class=foo] will output <a href="example.com" class="foo">example</a>.
  • parameters may contain spaces if you quote them: [link id=25 class="foo bar"] will output <a href="example.com" class="foo bar">example</a>
  • If DataProvider::findById() does not return a 'content' in its array, the parser will output <a href="http://example.com">http://example.com</a>

The code is too long to paste here, you can find it on gist. Just put each file in the directory specified by the first commented line and you should be set. Run example.php to see it in action. You can find out some more details about using this script by looking at the unit test.

kgilden
  • 10,336
  • 3
  • 50
  • 48