Saturday, March 10, 2012

Extract the first paragraph text from a web page with PHP


Using strpos and substr

Assuming the content to extract the paragraph from is in the variable $html (which may have come from a file, database, template or downloaded from an external website), use the following code to work out the position of the first <p> tag, the first </p> tag after that tag, and then get all the HTML between them including the opening and closing tags:
1$start strpos($html'<p>');
2$end strpos($html'</p>'$start);
3$paragraph substr($html$start$end-$start+4);
Line 1 gets the position of the first opening <p> tag
Line 2 gets the position of the first </p> after the first opening <p>
Line 3 then uses substr to get the HTML. The third parameter is the number of characters to copy and is calculated by subtracting $start from $end and adding on the length of "</p>" so it is included in the extracted HTML.

Converting to plain text

If the extracted paragraph needs to be in plain text rather than HTML, use the following to remove the HTML tags and convert HTML entities into normal plain text:
1$paragraph = html_entity_decode(strip_tags($paragraph));

Wednesday, March 7, 2012

recover Mac OS X Lion Password


Method 1 – Reset a Lost Mac OS X Lion Password

You have to boot from a Lion Recovery drive, this can either be a recovery disk made with the Lion Disk Maker tool, or just by booting from the built-in Lion Recovery HD partition.
  • Hold “Option” at boot and select the “Recovery” disk at the boot menu
  • Wait for “Mac OS X Utilities” menu to appear, indicating that you are booted into recovery mode
  • Click on the “Utilities” menu and select “Terminal”
Launch Terminal from Mac OS X Lion Recovery Menu
  • Type the following:
  • resetpassword
  • Confirm the user account and then the password change and reboot Mac OS X 10.7 as usual with your new password
This replaces the “Reset Password” menu item that used to be in place prior to OS X 10.7, which was one of two original methods of resetting a Mac OS X 10.6 or prior password. Why the change to the Terminal method? Probably for increased security now that recovery partitions are standard with Lion.
The above method is by far the easiest, but if it’s unavailable for some reason, you can choose another method:

Method 2 – Delete AppleSetupDone and Create a New Administrative Account

Mac OS X 10.7 does share a more untraditional approach to resetting a password as past versions of OS X. In this case, you can still delete the AppleSetup file which forces the “Welcome to Mac OS X” setup assistant to run again, which allows you to create an administrative account. You can then login to that new administrative account and reset your original account password.
From the Recovery Drive’s Terminal, type:
rm /var/db/.AppleSetupDone
Then reboot either through the menu item or by typing ‘reboot’ into the command line.
Follow the setup procedure as usual, create the new administrative account, and wait for Mac OS X to boot as usual into the new user account. You won’t see any of your familiar files or settings yet, and this is normal, because you have to reset the original password. Here’s how:
  • Open “System Preferences” and click on “Users & Groups”
  • Click on the lock icon in the lower left corner and authenticate, allowing you to make changes to user accounts
  • Select your original user account from the left side Users list, and then click on the “Reset Password” button on the right
  • Reset a Mac password from System Preferences
  • Enter and confirm the new password
  • Close out System Preferences and reboot
You can now login to the original user account with the new password you just set. Once logged into your original administrative account you can then return to User & Groups and delete the temporary admin account you created.
These two methods should work when booting a USB Lion install drive too, but it’s quicker to use the Recovery disk that is already active on OS X Lion installations.

Friday, March 2, 2012

Can't enable wifi on Toshiba

If the 'Flash Cards' don't appear at the top of the screen when you hit the 'Fn' key, then click the 'Start' button and type the word flash in the search field. In the programs that appear at the top, you should see 'Restart Flash Cards' and 'Settings for Flash Cards'. First, click on 'Restart Flash Cards', then again click on 'Start' and type in the word flash in the search field. This time click on 'Settings for Flash Cards', and make sure there is NOT a check mark on 'Disable all function keys', click 'Apply' and 'OK'.  Then try the Function key/Flash cards + your F8 key again.

If the above doesn't work download this file to your desktop, and double click to install it, and reboot. You do not mention if you are still running Vista 32bit, or if you have upgraded to a Vista or Win7 64bit edition, so only download the appropriate file.

Toshiba Value Added Package for Windows Vista/7 (32)

Toshiba Value Added Package for Windows Vista/7 (64)


After the reboot try the 'Fn' key plus your 'Fn' + 'F8' combination again, if necessary repeat the flash paragraph above. Please let us know if this helps. Good luck.

Monday, February 6, 2012

PHP Arrays

Numeric Arrays

A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In the following example the index are automatically assigned (the index starts at 0):
$cars=array("Saab","Volvo","BMW","Toyota");
2. In the following example we assign the index manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";

Example

In the following example you access the variable values by referring to the array name and index:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
The code above will output:
Saab and Volvo are Swedish cars.


Associative Arrays

An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.

Example 1

In this example we use an array to assign ages to the different persons:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

Example 2

This example is the same as example 1, but shows a different way of creating the array:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
The ID keys can be used in a script:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";
?>
The code above will output:
Peter is 32 years old.


Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Example

In this example we create a multidimensional array, with automatically assigned ID keys:
$families = array
  (
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
  );
The array above would look like this if written to the output:
Array
(
[Griffin] => Array
  (
  [0] => Peter
  [1] => Lois
  [2] => Megan
  )
[Quagmire] => Array
  (
  [0] => Glenn
  )
[Brown] => Array
  (
  [0] => Cleveland
  [1] => Loretta
  [2] => Junior
  )
)

Example 2

Lets try displaying a single value from the array above:
echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";
The code above will output:
Is Megan a part of the Griffin family?


Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.
The reference contains a brief description, and examples of use, for each function!

PHP tip: Comma Separated Values (CSV) List to Array and Array to CSV List



// create a comma separated list from an array 
$array = array('green','purple','blue','yellow','amber','red'); 
$comma_separated = implode(",", $array); 
echo $comma_separated; 

// create an array from a comma separated list 
$list = "green, purple, blue, yellow, amber, red"; 
$array = explode(',', $list); 
print_r($array);