Regular Expression to Find All Mentioned Names by ‘@’

December 12th, 2009 by junal Comments »

Most of the time we use ‘@’ to mention someone’s name in email thread, forum, etc. How about to find those mentioned names using PHP regular expression? Let’s consider the following text.

“Hey @Junal how about going for a vacation to @Srimangal? @Jewel said it’s an awesome place”
We will find all names that is mentioned by @ here. So let’s write the pattern first.

$pattern = “/^(?:[a-zA-Z0-9?. ]?)+@([a-zA-Z0-9]+)(.+)?$/”;

See, first portion of the pattern is optional, some one can start your name @junal or hey @junal right. And then we are matching with ‘@’ separating them from @ sign. Rest portion of the patter is quite easy, as we can keep text after the name (I.e @junal you suck)

Ok, now let’s use the php preg_match() function to find the matches. If we find the matched it will return 1 else 0.

$pattern = "/^(?:[a-zA-Z0-9?. ]?)+@([a-zA-Z0-9]+)(.+)?$/";
$str = "Hey @Junal hey how are you?";
preg_match($pattern, $str, $matches);
print_r($matches);
//Output : 
Array
(
    [0] => Hey @Junal hey how are you?
    [1] => Junal
    [2] => hey how are you?
)

So, if we want to find first matched name then it’s pretty straight forward to get it from $matches[1]
but what if we want to find multiple name from a text? Well, then we have to search $matches[2] if there is anymore mentioned name wit @ right?… let’s do it this way…

do {
   preg_match($pattern, $matches[2], $more_matches);
   $name_list[$counter++]  = $more_matches[1];
   $count = count($more_matches);
   $matches[2]=$more_matches[($count-1)];
   $more_matches = "";
   } while($count>=3);

this above code might be confusing. So let’s see the complete function that returns us an array of names that are mentioned by ‘@’ and well, we will also remove the duplicate names.

< ?php
/*
 * isn't the name self explanatory ?
 */
function giveme_names_from_at($str) {
    $pattern = "/^(?:[a-zA-Z0-9?. ]?)+@([a-zA-Z0-9]+)(.+)?$/";
    $str = trim($str);
    preg_match($pattern, $str, $matches);;
    if($matches) {
        $counter = 0;
        $name_list = array();
        $name_list[$counter++] = $matches[1];
 
        do {
            preg_match($pattern, $matches[2], $more_matches);
            $name_list[$counter++]  = $more_matches[1];
            $count = count($more_matches);
            $matches[2]=$more_matches[($count-1)];
            $more_matches = "";
        } while($count>=3);
 
        if(!empty($name_list)) {
            $all_names = array();
            $i = 0;
            foreach ($name_list as $key => $value) {
                if (!is_null($value) && (!in_array($value, $all_names))) {
                    $all_names[$i] = $value;
                    $i++;
                }
            }
        }
        return $all_names;
    }
}
 
//Example 
 
$str = "Hey @Junal how about going for a vacation to @Srimangal? @Jewel said it's an awesome place";
$names = giveme_names_from_at($str)
print_r($names);
 
//Output: 
Array
(
    [0] => Junal
    [1] => Srimangal
    [2] => Jewel
)
?>

Ok well, output shows what we needed. But using regular expression is not always a good idea as it’s slower than standard string manipulation functions. How would you like to find these answers without using RE? Or can we improve this pattern to make It smarter?

Dear Brac Bank Internet Service, You Suck!

December 6th, 2009 by junal Comments »

Really pissed off at Brac Bank internet banking service. I have been using it for last 1 year to see my transaction, debit, credit etc. Link that they gave me to log in, wasn’t even with a domain name. But anyways, that’s not my headache. I’m happy as long as I get the proper information that I registered for.

Few days back, I tried to log in to see my transaction and stuff and it said site is down or moved to a new address.

Site was moved in a new address

Site was moved in a new address

I tried it after 2 days again, but no information regarding the change. Shouldn’t have the informed me that they moved to a new location? Don’t I deserve it as their customer? So I sent an email to their IT dep and asked about it, they replied saying …

“….This requires a higher level of security for our systems and processes. Therefore, we are asking all previous I-banking subscribers to register once again to ensure that the new security standards are maintained.

after reading this, first thing came in my mind was like “WTF”, why the hell I have to register or fill up a form again? Don’t you already have my all information. I have never seen this kind of service before from any site!

But what could I do, I went to Brack Bank and registered for it again. And today they sent me a new link and log in information to use the internet banking service. I was happy yay! But hey, when I went to log in from the actual link it says “This site only supports Internet Explorer”! What?!? I don’t use IE and when I have to use I use it in VB XP. Do you think I should login to XP just to use your service dear BB?

Sorry i dont use IE for browsing...

Sorry i dont use IE for browsing...

I’m sorry, but this is complete bullshit. Don’t Firefox and Chrome have Anti-fishing capability? Or .NET framework (site is developed on .NET) always asks you to restrict users with IE?

I don’t know what option I should choose now. But Brac Bank should be careful about their customer serive. Till now it sucks!

Javascript: Object Detection or Browser Detection?

November 21st, 2009 by junal Comments »

This question can be answered easily with “object detection” – why? Because some obscure browsers won’t be treated correctly and browsers that appear after you’ve written the page may not be adequately covered either as a result you will end up with some error in some browsers. Object detection works fairly in all browsers.

But, certain features of Javascript don’t work in all browsers. As an example “innerText” property doesn’t work in Firefox but in IE, Chrome Etc…this problem could be solved easily like the following ..

var text = x.innerText || x.textContent
//innerText for IE or other browsers and textContent for Firefox that works well.

Now, consider the following statement

Google Chrome doesn’t read encoding information that’s declared with document.write(). If you’re using this method to declare encoding in iframes, for example, you may see garbled characters when the iframe is rendered. Instead of:

document.write("<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">");
    ... other JavaScript code ...
</meta>

and when it might arrived? Let’s say you are using a Rich Text Editor where you will have onload events with document.write() and you will notice that iframe attribute (e.g name) value is not getting in the browser. Rather Chrome is setting name attrribute value from itself.

There is a tricky way to get it solved, in my case I used Firebug extension to get the attribute value and then detect the browser. Well in that case object detection doesn’t work for me. I must use browser detection right.

Let’s have a look at the following codes to get some idea about what I have been talking…

function notEmpty(){
 
        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        var textvalue = '';
        var framename = '';
        var elementValue = ''; 
 
        if(is_chrome) {
            elementValue = window.frames['Rich text editor'].document.body.innerText;
        }
        else if(navigator.appName =="Microsoft Internet Explorer") {
            elementValue = window.frames['myIframe'].document.body.innterText;
        }
        else {
            elementValue = window.frames['myIframe'].document.body.textContent;
        }
 
        if(elementValue =='')
        {
            //do what you want
            return false;
        }    
        return true;
    }

Conclution: It depends, generally it’s always good to use object detection no doubt. But when you have no option? Well then don’t wait to use browser detection eh… ;)

Check IFRAME Text by Javascript

November 20th, 2009 by junal Comments »

Most of the Rich Text Editor use iframe, and besides server side validation it’s necessary to check client side validation with JS before storing any data. Let’s say you want to check if the body of iframe is empty. This is a little bit tricky, like you can easily check a textarea value with document.getElementById(idoftextarea).value; but it won’t work in case of iframe. Let’s consider the following scenario…

<iframe name="richEditorIframe" width="100%" height="25%" src="test1.txt">
</iframe>
 
<form name="formName">
     <textarea name="textarea" cols=80 rows=18 id="textArea">
         This is a test
     </textarea>
      <br />
      <button type="submit" value="Submit" />
</form>

Now, to check if iframe is getting in Database with empty value we can make a function like the following….

<script type="'text/javascript'">
    function checkEmpty(){
        var elementValue = window.frames['richEditorIframe'].document.body.innerText;
        if(elementValue=='')
        {
            //say something here
            return false;
        }
        return true;
    }
</script>

instead of innerText you can use innerHTML but then in most of the Text Editor it will show some HTML element which means you are not getting empty result even if you didn’t write anything. So, innerText will give you the iframe body value if it’s written. Now let’s call this function onSubmit in the form…

<form name="formName" onSubmit="return checkEmpty();">
      <textarea name="textarea" cols=80 rows=18 id="textArea">
          This is a test
       </textarea>
       <br />
      <button type="submit" value="Submit" />
</form>

That’s it, now it’s simple :)

Welcoming All Bangladeshis to “Amra Positive”

November 8th, 2009 by junal Comments »

I promised to release “Amra Positive” soon, today i’m announcing the release of this small application that is developed for Bangladeshis. Let me give you some ideas about the application first…. [i'm just coping the “about” page from the application]

“amra positive” is a place for Bangladeshis to leave good and positive news about Bangladesh, friends, family, events etc. no pressure, but it’s up to you to leave something good behind within 150 characters. there is no moderator to modulate your posts in this application. if you don’t like to post or don’t have anything to say, you can still vote on others; posts. so let’s have a look what you can do here…

  • you can post a message about the country, your friends or family, events that you liked or think is positive

  • you can vote on your own posts and others’ posts. currently there is no commenting system. you can only vote positive or negative about a post.

  • you can tweet a post if you like. links will be automatically shortened, you don’t have to worry about that.

  • you can check posts that were voted the most positive from users from “top10″ tab

  • as an owner of the post, you have access to delete it

  • you can check all your past/previous posts by clicking the “authors” tab or by clicking your nick name

  • clicking on a message will show that specific post in an individual page

  • you have to use your gmail id to sign in. please note: that password is not shared with the admin of this application, only the gmail id is shared. so there is nothing to worry about

  • all text in this site in lowercase to make it look a little bit more different.

    Home page of Amra Positive

    Home page of Amra Positive

in addition, you can subscribe to the most recent and top 10 posts by clicking on rss button. there is currently no feedback system right now. but if you have any feedback, comments, suggestions or concerns just email me at junal@junalontherun.com

p.s : this is a beta version of this application. looking forward to hearing from you. report any bugs in the application and let me know whatever is on your mind by email. thanks for your patience and support with this idea!

Link : http://amrapositive.appspot.com/

Get Adobe Flash playerPlugin by wpburn.com wordpress themes