Recently in PHP Category

I was given today a little project, as a freelancer sometimes you have to prepare for the unthinkable even though their title begs to differ. I mean today it's been a spacey day, wasn't that bad since I learned something too.

So, you must be wondering what happened? If you are a developer, imagine 3,000+ \r\n invading the HTML source code. Plus different \r\n with 3,4,6 spaces, obvious the designer who did the layout surely like to annoy others. \r is carriage return while \n is newline, windows style, I think.

What does this do? Nothing, you can't see it when you are displaying the HTML, but if you check the source code then you notice big spaces and carriage returns.

Solution? My solution is pretty straight foward, yet it lacks efficency.

In the PHP you must get the HTML in a variable.

Then to remove the excessiveness our smart designer added:

$regex = preg_replace('{^[^>]([\r\n])?( | )?([\r\n]+)$}mx','',$html);
$regex = preg_replace('#([ ]+[\r\n][\r\n])#x','',$regex);
$regex = preg_replace('#(([\r\n]{3,}|[\r\n]{4,})([\r\n]+))#x','
',$regex);

I started by telling the regex engine to replace after > is set, search for possible returns and newlines is there are any present, search 3 spaces OR 1 space, optinally. Search for one or many \r\n and replace them with nothing.

Later on the second preg_replace, check for one or many spaces followed by two /r/n

And lastly, search 3 /r/n OR 4 /r/n to unlimited, followed by one /r/n+ and remove them.

I think I could have done better, but since deadlines are not something you can negotiate with--well, you get the idea. This got it done and that's all it matters. Luckily, I compared the line numbers, the original file had 4312 lines and the modified one ended with 313 lines.

Break Brains

Cheers

videoCast PHP Parser

| | Comments (0) | TrackBacks (0)

Well, here I am again, with a working parser that might be helpful for your development, especially if you are looking for a youtube link parser.

What is this?

videoCast PHP Parser is a pretty simple class that helps you parse links like http://vimeo.com/613460 into an embedded HTML. It supports Youtube, Dailymotion (permalinks only), Metacafe, Revver and vimeo.

How to use?

This is the easy part. Upload the parser to your site and in your php file.

require_once 'viCastPHP.php';

$parseVideos = new viCastPHP();
$parseVideos->getFormat($_POST['text']); // You will need to change this and adapt it to your needs

Another way of using this parser is

$parseVideos->limitParse(3)->excludeSites(array('youtube','metacafe'))->getFormat($_POST);
or
$parseVideos->limitParse(3)->getFormat($_POST);

getFormat must be called last for it to take effect of the changes.

excludeSites() only accepts arrays or a plain string.Right now the strings you may put in the array or string are only: "youtube","dailymotion","revver","vimeo","metacafe".

limitParse() only limits # for each pattern. (I'm thinking of extending this but that depends now)

What was this for?

I created this for learning purpose for both PHP5 OOP and Regular Expressions. I thought I'd share it with you guys :).

Get the parser NOW!

Yesterday out of boredom I started playing with Regex (Regular Expressions). I somehow got carried away and found that regex is actually fun once you learn it. It's like playing chess, all the strategy you have to do to match and process your text is part of winning over content.

Well, what I did is pretty little, that is converting plain urls like

http://www.youtube.com/watch?v=PF-cXjxgREQ

To

It also supports links with http://au.youtube.com/watch?v=PF-cXjxgREQ and so on. Of course youtube alone wasn't enough so I threw at it Metacafe, revver, dailymotion, vimeo support.

Of course, an issue came up with dailymotion, that is the way the "categorize", so the URL is affected and it only works with Permalinks. I'm currently working on a fix that might actually fix the redundancy of "/section/section/section/".

Try the parser, live!
Paste links like

http://www.youtube.com/watch?v=pkuLh4u0H8E
http://www.dailymotion.com/video/x5qbhc_99-dreams_shortfilms
http://www.metacafe.com/watch/1269694/trick_shot_masters/
http://revver.com/video/1076971/sexy-fitness-break-tight-glutes/
http://vimeo.com/707539

Paste them inside the textarea :) (of course!) Anyway, this will be available under New BSD License as soon as I finish a few things.

I'm still new at regex! Can't wait to master it soon :)

Tuning up

| | Comments (0) | TrackBacks (1)

For the time being, I think I ought to at least post once a week, I know that this blog is almost non-existence at some point. Well, never mind about that for now.

Just a few weeks back I ordered a book called "Mastering Regular Expressions" by Jeffrey E.F. Friedl, it arrived this monday and I was really excited because, as geeky/nerdy as it sounds, regular expression is not something you learn on the fly. I normally script using PHP, although I want to extend my knowledge, it's very TIME CONSUMING just having one language to learn (PHP, in this case).

I have known/used PHP for around 2-3 years. Yet, it's not enough to actually "master" it. Of course, I'm not merely talking about syntax and learn more of the functions it provides, but also learning the design patterns. (Object Oriented Programming)

Anyway, the book "Mastering Regular Expressions" is really a new experience, the book itself boosts your confidence and motivation on learning. The author maintains the context both entertaining and very well detailed on how metacharacters and regular expressions works.

I know sometimes people from other linux forums steps here by occasion, and I'm expecting something near the lines "learn python, perl, ruby(RoR)" (web side scripting). Well, it's not a matter of choosing the language sadly, right now I just want to shape up my PHP skills before taking on another language.

Besides that, I also ordered Simply Javascript. Although there is a bit of problem for the package to reach here :( damn you!

About this Archive

This page is a archive of recent entries in the PHP category.

Personal is the previous category.

Real World is the next category.

Find recent content on the main index or look in the archives to find all content.

August 2008: Monthly Archives

Pages

Powered by Movable Type 4.1