PHP regular expression issue

Fun with perl vs php regular expression handling.

PHP has a regular expression search and replace function, preg_replace. This is supposed to be the standard greedy algorithm for patterns, for example, A* means zero or more A characters, and it matches the longest string of A's at that point.

So, if you want to match zero or more slash (/) characters at end of a string the pattern to use is: /*$

And, if we want to replace zero or more slash characters at end of a string with a single slash character, the perl code is:   s!/*$!/!

Full example in perl:

foreach ('path/to', 'path/to//', 'path/to///', 'path/to////') {
    my $s = $_;
    $s =~ s!/*$!/!;
    print "string: $_ changed to $s\n";
}

Output is: 
string: path/to changed to path/to/
string: path/to// changed to path/to/
string: path/to/// changed to path/to/
string: path/to//// changed to path/to/

And the above output is all correct.

Equivalent PHP code does not work, using PHP version 5.1.6.

Following PHP code:  $t = preg_replace('!/*$!', '/', $s); fails, it will end up with one / if the input has 0 or 1 / characters, but it will end up with two / characters if the input has 2 or more / characters, instead of a single / character. So, the match was not greedy, but for reason, was split into two matches, and then each matched group was replaced with a single / character.
The workaround for this is to specify a limit count of 1 to preg_replace, which makes the code work fine. preg_match seems to work fine, only preg_replace has this problem.

Here's php code, and its output, showing the failure:

<?php
foreach (array('path/to', 'path/to//', 'path/to///', 'path/to////') as $s) {
     # preg_match('!/*$!', $s, $matches); // works fine, is greedy - $matches[0] is zero or all slashes
     # $t = preg_replace('!/*$!', '/', $s, 1); // works, limit of 1 helps

Flor De Mayo

Flor de Mayo 2651 Broadway, between 100th and 101st streets, The Upper West Side.
Also at 484 Amsterdam Ave, between 83rd and 84th streets.
Spanish & Chinese Cuisine

Food Rating: 4 stars/4 Value Rating: 3 stars/4

Their rotisserie chicken is superb. From the menu, Pollo a la brasa - Peruvian Special Chicken, Whole - "looks like a regular roast chicken, but the difference is our special ingredients". And this is true, fantastic chicken, nicely spiced (lemon? garlic? cilantro?).

Have not eaten anything else from their extensive menu, always go for the takeout - "two whole chickens cut-up". The menu has Peruvian dishes, Hong Kong Special items, Spanish items, as well as the standard Americanized-Chinese dishes.

This restaurant find is courtesy of the New York Times article "In Quest of the Perfect Roast Chicken", dated February 23, 2005, by Julia Moskin.

Video recording, editing, creating DVD

Nothing like fiddling with MPEG packets on a rainy Saturday afternoon!

This post will be periodically updated, until a reasonably easy, scripted list of steps is documented, on how to make a DVD out of video recorded on a Linux system.

TV Receiver and MPEG2 Encoder: Hauppauge WinTV-PVR-150 (MCE Editon)
This is supposed to be for Windows Media Center Edition only, so will not install on any other Windows operating systems, but works fine on a Linux computer! There is something amazing about that sentence - will not work on Windows, works on Linux! How far has Linux come...

Software: dvdauthor, avidemux2, mkisofs, growisofs, ivtv-drivers, xine, etc
And running on a Fedora FC5 Linux system.

The goals of the steps are to use scripts to save MPEG encoded video, and then perform simple editing - cut out portions not needed, and create a simple DVD structure. Avoid transcoding of video - sure, it is technically possible to get lower bit rates from higher bit rate video, but the quality reduction using transcoding is pretty drastic (possibly because it is very complex with many possible ways to do this), so best to capture at rates desired, and make sure no intermediate step involves transcoding.

Procedure:

  1. Record video as needed, using the script shown elsewhere here - copy video for given duration. In the scheduled command, use ivtvctl commands to set bitrate, tune to correct channel. Choose a DVD-compatible bitrate, for example, I use 6Mb/sec CBR for capturing NTSC Standard-Definition video.
  2. Load up the clip in avidemux2, cut out all ads or portions that are not needed. The avidemux2 pages have good tips on how to make cuts that wll allow the video frames to be just copied - place both A and B marker on I-Frames of the MPEG stream, and then cut.

Daily Groceries

Grocery shopping for this creature of habit.

My weekly grocery bag, here are all the things that I usually get:

Good Stuff

  • Brown Cow non-fat plain yogurt.
    This brand is the best - perfect amount of sourness, right consistency, great taste. And for once-in-while binging, nothing beats the Cream-On-Top variety. I stick to the plain varieties, no need to indulge in the sweetened or fruit flavored versions.
    The non-fat plain yogurt is on my daily menu. Here's a link to Brown Cow website. This brand is available in the local Whole Foods Market.
  • Zeppy's Bagels.
    They come from a bakery called Zeppy's Baigel Bakery, and the bagels are sold in a few local stores, for example, in the local Whole Foods Market. The nice thing about these bagels is the right amount of chewiness - not too hard, and a great taste - unlike the totally bland, tasteless bagels served at many local stores. This is very popular in the local store, so many times there are no Zeppy's Bagels left late in the evening.

Guilty Pleasures

  • Butter-Rum muffins from the local FoodMaster.
    Don't even ask how many calories for each muffin, half-a-muffin is probably the right serving for once every two days! Having tried so many muffins from so many local bakeries, this was a incredible, serendipitous find - local grocery chain with heavenly muffins! These are made fresh daily, and invariably, by late evenings sometimes even by afternoon, the butter-rum muffins are all gone, so have to get them early. The FoodMaster also increased the cost of these muffins 40%, and made them slightly smaller in 2006! Other types of muffins are still the old price, so I suspect they have caught on to having made a hit with these butter-rum muffins.
  • Banana-Nut Bread, from the local Stop&Shop
    Another find that is unexpected - local grocery chain, with pretty good banana-nut bread.

Spammers are here

Like clockwork, these pests are able to locate new sites on the web, to infect with their spam postings!

For the past two years, I've not updated these pages much, so had escaped the attention of spammers. But just a month ago, when I got back to updating this site regularly, the spammers have woken up and now I've to keep a closer watch on the new posting activity here.... sigh...

Currently just using the spam module in drupal, which is working fine - though may need to change the configuration so that it marks things as spam as soon as a single URL is found in the post.

Rajma, Rice, and bibaDa

This happens to be a Punjabi dish, and my recollection of eating this in Maharashtra and Gujarat is in a combination of Rajma, Rice, and bibaDa.

bibaDa (बिबड़) is a Jowar pappad (ज्वारी, sorghum pappad) - certainly can't get it anywhere in the US, nor it is easily available in India, have to travel to interior towns in Maharashtra to find someone who even knows about this. Like pappads, it is a crispy snack, can be baked or fried. I prefer the baked variety - put this in a toaster oven, watch it very carefully, one or two seconds more in the toaster oven, and it will burn completely to black. Cooking time in a toaster oven is around 30-60 seconds - keeping a watch all the time. It has a smoky, crispy taste, and closer to a tortilla chip than to the more popular chickpea pappads.

Rajma (राजमा) is kidney beans - an amazing store house of good nutrients - high soluble fiber, fat-free high quality proteins, minerals, and lots more as a search on the web reveals, and in the curry dish below, with rice and bidaDa, it tastes great too!

Rajma (राजमा) Curry

Time: 2 hours
Serves: 4-6

Red kidney beans soaked overnight - 2 cups
Onions, finely chopped - 1-2 large
Tomatoes - 1-2 large, chopped
Ginger and Garlic - 1 tablespoon each, finely chopped
Cumin seeds - 1 teaspoon
Red chilli powder - as needed
Coriander Powder - 1 teaspoon
Cumin Powder - 1 tablespoon
Garam Masala - 1 tablespoon
Oil - 3 tablespoons
salt to taste
finely chopped coriander leaves for garnishing
5-7 cups water

Yahoo still no good

I used to use Yahoo a lot, and now go there only for news and email - not for search, and will probably switch over for those old legacy uses as soon as I find something else.

In Firefox, if I go to the Yahoo search page, it would tell me:

Use Yahoo! to search from Firefox

which is something I am never going to do - for a long time now, Yahoo search results have always excluded mention of any aczoom.com link, other than somewhere way below in the search results. Given this, no way am I going to switch my Firefox search box to Yahoo - Google is much better.

This is why I presume there are so many pages on the web related to "SEO" - trying to figure out what search engines do, and how - it is too complex. For example - how come a search engine can list 100s of pages that all link-back or refer to the main page on a topic, but never have that main page show up higher on the search results?

aczoom.com uses Drupal and I suspect this issue is related to that, and URL re-writing is confusing the Yahoo bots. But there are also static pages at this site, and Yahoo does not show them either. And both Google as well as Microsoft MSN search get it right - for all Drupal hosted pages here, as well as the static pages.

So, Yahoo shall remain unused in the Firefox browsers that I use. Hopefully, this should have Yahoo engineers all concerned and rush to fix their search engine :-)!!! Until then, I'll stick with Google Search.

Older blog entry related to same topic: search engine fun

Steel Cut Oats

Had enough of rich buttery high-fat foods, loaded up on red-meat and sugary snacks, and now looking to fix everything wrong in your body?

Have no fear, oats to the rescue - it has soluble fiber to help reduce harmful cholesterol. So, fix yourself up with a regular serving of oats. Additionally, filling yourself with oats means less space in your stomach for all that other stuff that you crave but is not good for you.

Rolled oats, as well as instant oats, in my opinion, don't taste very good - soggy, no texture, insipid food. Steel-cut oats are a world apart - they have a hearty taste, nice chewy and nutty texture - good taste, and good for you - and there is no catch!

Steel-Cut Oats are whole grain groats (the inner portion of the oat kernel) which have been cut into two to three pieces.
A popular brand is McCann's Irish Oatmeal. But for those eating this regularly, look for steel-cut oats in your local bulk-grocery store - bulk-bin purchase, in stores such as Whole Foods, will get you 5 times more oats for the same price.

There are many ways to cook steel-cut oats, here's the way I cook them. Other alternatives for faster cooking involve soaking overnight (in hot water - in cold water it does not help at all), or using the pressure-cooker.

Each quarter- to one-third-cup dry steel-cut oats is one serving, around a cup of cooked oats.

Time: 40 to 60 minutes - but varies with size of the cut - larger grains take longer time. Just be very careful, overcooking makes everything too mushy and very easy to overcook in a covered pot and under-cooking oats taste too raw.

Servings: 6-8

1.1 to 1.5 cups steel-cut oats
4-5 cups water

Bring the water to a boil in a covered pot. Good to use a glass cover to watch when oats start to boil over in the next step.

To the boiling water, add all the oats.
Pot should be watched when boiling - use a large pot, and lower the heat to medium, to prevent it from boiling over.

Updating kernel and consequences

On a Fedora FC5 system, it is very easy to update the software using yum update.

This also updates the kernel, which is good, but given the number of "non-standard" items installed on my system (VMWare Server, NVidia Display, PVR-150 TV Tuner/MPEG Encoder), it requires a lot of fixing up after every yum update. [So I have automatic update disabled, do the update only when time permits to do all the fixups.]

After a kernel update:

  • Check to see that the matching kmod-nvidia-* module also made it in, otherwise the display won't work well. In some cases, the kmod-nvidia module site is down, so may have to build module by hand.
  • yum install ivtv ivtv-kmdl-`uname -r`
    to get the driver for the PVR-150 TV-Tuner/MPEG Encoder card.
    This should also install the firmware, if it does not, visit the ivtvdriver pages to do the firmware install manually.
  • If mplayer has problems with audio, check alsa-lib. I have the atrpms site listed for updates, and that is considered risky, packages may not match the other sites. In Sep 2006, started to get this error when using mplayer:
    alsa-lib: pcm_direct.c:1632:(snd_pcm_direct_parse_open_conf) Unknown field ipc_sem
    The fix is to remove alsa-lib:
    yum erase alsa-lib.i386
    (which may uninstall packages like firstboot, and control-center,m which I don't use )
    and then re-install it using this:
    yum --disablerepo=atrpms install alsa-lib
    (and if needed, reinstall firstboot, control-center, etc).
  • Rerun vmware-config.pl

vaa.ngyaachii bhaajii (वांग्याची भाजी)

Serve with rice and varan (yellow lentil soup), or with chappati.

Time: 30 minutes
Serves: 3-4

Eggplant - 1 large, to fill 10" saute pan (but use larger pot for cooking)
Chop into small pieces (0.5-1" cubes)
Black Mustard Seeds - 1/2 teaspoon
Cumin Seeds - 1 teaspoon
Turmeric - 1/2 teaspoon
Green Chillies - chopped, as needed
Salt
Water - 1/2 cup
Oil for frying

Heat oil
Add Mohri (black mustard seeds), when pops, add Jeera (cumin seeds)
Add chopped green chillies, to taste - should be hot/spicy
Cook for a minute or so, then added chopped eggplant, with turmeric.
Coat eggplant well with the cooking oil, and then add water - half a cup
or less, cover and let it cook - 10-15 minutes.
Add more water if needed, then add salt to taste.