Search Blog Posts

Posts Tagged ‘string manipulation’

PHP capitalising the first letters of each word in a sentance

Friday, June 27th, 2008

I recently wanted a contact form to convert a form element, in this case someones name to uppercase. For example ‘tom peters’ decided to enter his name without any capitals. The desired result would ideally be ‘Tom Peters’.

Using PHP we can convert the form field variable by doing the following:

$personsname= "tom peters";
$PersonsName = ucwords($personsname);

echo "<p>The old string - $personsname <br />";
echo "The new string - $PersonsName </p>";

There are a few more examples that you could experiment with for example:
strtolower(); will convert a string to lowercase.
strtoupper(); will convert a string to uppercase.

Returning the Current Page URL

Sunday, March 2nd, 2008

Using PHP you can return the current url of the page by simply using :

$pageURL = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
echo $pageURL;

By using string manipulation you can also us this function to return the end characters of the url request.

For the following url: http://minimalistics.co.uk/gallery/main.php?g2_itemId=89 we can return the end numbers using the following code:
$pageURL = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$pageURL = substr($pageURL,strrpos($pageURL,"=")+1);
echo $pageURL;

The result of this would be 89.

© Copyright 2003-2008 Minimalistic Designs | Terms | Links | Sitemap
Website Design Liskeard - www.minimalistics.co.uk