...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
May 28, 2012

PHP Currency Regular Expression

Question by Lee

I am trying to find a piece of regex to match a currency value.

I would like to match only numbers and 1 decimal point ie

Allowed

  • 10
  • 100
  • 100.00

Not Allowed

  • Alpha Characters
  • 100,00
  • +/- 100

I have search and tried quite a few without any luck.

Hope you can advise

Answer by The Pixel Developer

if (preg_match('/^[0-9]+(?:.[0-9]+)?$/im', $subject))
{
    # Successful match
}
else
{
    # Match attempt failed
}

Side note : If you want to restrict how many decimal places you want, you can do something like this :

/^[0-9]+(?:.[0-9]{1,3})?$/im

So

100.000

will match, whereas

100.0001

wont.

If you need any further help, post a comment.

PS If you can, use the number formatter posted above. Native functions are always better (and faster), otherwise this solution will serve you well.

Answer by Starx

How about this

if (preg_match('/^d+(.d{2})?$/', $subject))
{
   // correct currency format
} else {
  //invalid currency format
}
Read more
May 27, 2012

Push associated variables into array in PHP

Question by Dev Newb

I have a loop and each time the loop runs I need to add two variables to an array. What I am trying right now is:

$attach_array['outline'] = array();

foreach ($_POST['attachment'] as $key => $value) {
  $attachmentName        = $value['name'];
  $path                  = "1";
  $name                  = "alsdkjf";
  $attach_array['outline']['path']=$path;
  $attach_array['outline']['name']=$name;
}

Then later in the script I try to get these values out for PHPMAILER:

foreach ($attach_array['outline'] as $key => $value) {
   $mail->AddAttachment($value['path'], $value['name']);
}

This and other attempts are not working so I’m hoping for some help on putting $name and $path into an array in my first loop to use later.

Answer by Starx

You are overriding the same variables on each loop. You should do something like this:

  $attach_array['outline'][] = array('path' => $path, 'name' => $name);

By doing this, now all the path and values will remain on the array as separate items. You dont have to change the code you are using it from.

Read more

How can I replace part of a string which is wider than 50 px with "…"?

Question by Snehal Kumar Mishra

I am looking for a function which replaces part of a string which is wider than 50px with "...". I want to implement it in Javascript/jQuery.

Example:

  • Say I have a variable var="Theta Saving Non-Qualified Plan"; I want to put a restriction on the length of the string (based on pixel width). If the length of string is more that 50px, then the part of the string which is from the 51st pixel to theend of string will be replaced with "...".

Any idea guys?

Answer by Starx

This cannot be deduced as easy as you would like it to be. Because how much width a character or set of characters will take will depend upon the font-size. And calculating a text width is hardly accurate.

So, its better you create your interface to be friendly with characters rather than the width.

var t = $("#div").text();
if(t.length > 50) {
    $("#div").text(t.substr(0,50)+"...");
} 
Read more

Using multiple ='s for IN?

Question by David542

What would be the difference between doing:

SELECT person FROM population WHERE id = 1 or id = 2 or id = 3

and –

SELECT person FROM population WHERE id IN (1,2,3)

Are they executed the exact same way? What difference is there? Would there ever be a reason where one would you IN rather than multiple =‘s?

Answer by Starx

No, they perform the same thing. The IN minimizes the query string. That’s all. Such statements help in query optimization.

One difference in these two comparison operators would be that IN uses a SET of values to compare, unlike the “=” or “<>” which takes a single value.


According to the manual:

if expr is equal to any of the values in the IN list, else returns 0.
If all values are constants, they are evaluated according to the type
of expr and sorted. The search for the item then is done using a
binary search. This means IN is very quick if the IN value list
consists entirely of constants.

Read more
May 25, 2012

Looping through a complex php array

Question by techish

Array (
  [0] => Array ( 
    [event_title] => Registration
    [event_id] => 17
    [location_id] => 113
  )
  [1] => Array (
    [event_title] => Sunday Collections
    [event_id] => 67
    [location_id] => 113
  )
  [2] => Array (
    [event_title] => School Tuition
    [event_id] => 68
    [location_id] => 113
  )
)

Can anyone tell me how to extract event_title, event_id and location_id from this array? I want to display this in the form of a table actually.

Answer by Mischa

To display it in a table, you can do this:

<table>
  <tr>
    <th>Event Title</th>
    <th>Event ID</th>
    <th>Location ID</th>
  </tr>
<?php foreach($array as $item): ?>
  <tr>
    <td><?php echo $item['event_title'] ?></td>
    <td><?php echo $item['event_id'] ?></td>
    <td><?php echo $item['location_id'] ?></td>
  </tr>
<?php endforeach; ?>
</table>

Answer by Starx

The array is very simple in fact.

foreach($array as $item) {

    $event_title = $item['event_title'];
    $event_id = $item['event_id'];
    $location_id = $item['location_id'];    

}
Read more
May 24, 2012

Numbering with prefix of zero in PHP

Question by Juvar Abrera

Is it possible in PHP to echo out a number with zeroes at the beginning?

For example

<?php

$i = 0001;
$i++;
echo $i;

?>

And when it print out, I want it to be like.

0002

Is it possible? Thanks 🙂

Answer by Starx

Yes, it is possible. You can do this using str_pad() method. Basically, this method adds a given value till a required length is achieved.

A Basic example of this would be:

echo str_pad($i, 4, "0", STR_PAD_LEFT); 

Here,

  • 4 represents the output length
  • "0" represents the string used to pad, till the length is achieved.

Demo Example:

<?php
$i = 0001;
$i++;
echo str_pad($i, 4, "0", STR_PAD_LEFT);
Read more
May 21, 2012

Correct way to change user table relationship?

Question by user1357684

I am working on an event system that has two tables, EVENTS and EVENT_CREATORS. I have been linking events to creators by placing creator id in the events table as I thought I would only ever have one event creator.

I now need the ability for more than one creator to edit an event. Should I add simply add additional fields to EVENTS table, i.e ec_id, ec_id_2, ec_id_3, etc. Or does it make more sense to add a cross reference table to the database and a separate table for additional creators?

Answer by Starx

This is those cases, where it would be wise to use a cross reference table. I will explain it step by step. First

  • Create a new table. Call it “event_reference”
  • Give the following FIelds: Id, Ref_Id, Creator_ID.

I will omit the need of the EventId, because we are creating a table which is a reference to the event, so event’s table will hold the Ref_Id to keep in track of all the references.

  • Next, Modify the events table and store Ref_ID instead of Creator

In such way, you can fetch all the creators of an events in the normalized way.

Read more
May 19, 2012

Jquery smooth scroll external anchor link

Question by user1405690

okay can you please help me with a peice of jquery code. Here is what i want to happen. My website is cyberfanatic.com right so. So for example lets just use the footer for now. i have a a tag with the id of footer right. so say if someone visits cyberfanatic.com/#footer right now the page will load and the then jump directly to the bottom of the page to where the anchor link with footer is. How can i get it so that when the page cyberfanatic.com#footer loads that the page loads and the smooth scrolls to the anchor with the id of footer. Please help me out.

Answer by Starx

Use the scrollTo() plugin function

$.scrollTo("#footerid");
Read more
May 18, 2012

Javascript event after the dom is ready but not rendered

Question by rematnarab

Is it possible to do something after the dom is ready but it is not rendered(White screen)
I would like to hide the contents from user and after some operations i would like to show the final picture.
I could use “display:none” on my body tag but i am working on a huge project so i dont want to change every page.
Thanks

Answer by Starx

Here is how?

document.onload = fucntion() {
    //your codes
}

Unlike, window.onload this function runs after the DOM is loaded, so the manipulation is possible, but it does not require all the elements to be rendered.

Read more

What jquery video player, plays avi, mp4, mkv, wmv files?

Question by crosenblum

I am working on a web-based media center app. As a lightweight alternative to having a running a media center app in linux.

I have a bash script that generates an html file that uses a polaroid display, of all the posters of my downloaded movies.

http://imageshack.us/photo/my-images/651/polaroidscreenshot.png

Once I click on a movie poster, I want to go to a second page, that will play a locally hosted video.

I have tried working with jplayer, but have had problems just using the demo code, that it comes with.

Are there jquery or html5 video player’s that support standard movie video formats? Such as avi, mp4, mkv and wmv?

Or would I have to convert them to a format that is more usable to the player?

I would rather not have to convert them, that is doable but would be time consuming, and may distort video quality.

Any suggestions?

Answer by Starx

Why?, there are already web friendly formats of the video. flv, mp4, webm are very good formats, can support high quality too. You don’t need to focus on creating such things.

You can use, media player widgets on the website, and let the video play if the client has the supported codec to play the video. But, since this will narrows the visitors to a windows platform, I dont recommend it either.

With that being said, there is a plugin called jQuery Media Plugin which provides a limited of what you want.

Read more
...

Please fill the form - I will response as fast as I can!