Written by Sai Sandeep Thota on September 28, 2013 in php > Programming

Extract leftmost characters until a space using PHP

GET ALERTS:

Get our Latest updates delivered to your mailbox!

This is our first article in PHP programming category. PHP is a general purpose programming language useful for Web development, PHP stands for Hypertext Preproccessor. Most popular websites are written in PHP including WordPress. In this article we will be learning How to extract leftmost characters until a space using PHP.

For instance, if we have a string: “Sai Sandeep”, we want only the leftmost characters before a space, i.e., we want only “Sai” part. If you have noticed, Facebook has recently implemented this in the navigation bar. Previously it used to show your full name, recently it has changed to show only a part of your name.

facebook-navigation-bar

Extract leftmost characters until a space using PHP

#1. First let us create a database table:

CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM ;

#2. After that, populate the table with some names.
#3. We will need two functions to perform this, they are: substr and strpos which are used to get a part of string and get string position(integer) respectively.
substr general syntax is: substr('sandeep', 0, 3); gives san, i.e., we are fetching characters between 0 through 3.
strpos general syntax is: strpos('sandeep', 'n'); gives 2.
Using strpos we are searching for blank space in the string and as the function returns an integer, so we are using strpos in substr.
#4. Connecting to the database:

$con=mysql_connect('localhost','root','')or die("Cannot Connect!!");
mysql_select_db('PUdemo')or die("Cannot select db!!");

#5. Selecting and fetching records:

//Selecting records.
$sql = "SELECT * FROM users";
$results = mysql_query($sql);

//Fetching records.
while($row = mysql_fetch_array($results))
{
	echo $row['name'].' - '.substr($row['name'], 0, strpos($row['name'], ' ')).'<br>';
}

In the 8th line, we are using substr and strpos.
#6. Complete code:

<html>
<head>
<style>
body {
	font-family: Verdana;
	font-size: 12px;
}
</style>
</head>

<body>
<?php
//Database Connection.
$con=mysql_connect('localhost','root','')or die("Cannot Connect!!");
mysql_select_db('PUdemo')or die("Cannot select db!!");

//Selecting records.
$sql = "SELECT * FROM users";
$results = mysql_query($sql);

//Fetching records.
while($row = mysql_fetch_array($results))
{
	echo $row['name'].' - '.substr($row['name'], 0, strpos($row['name'], ' ')).'<br>';
}
echo strpos('sandeep', 'a');
?>

</body>
</html>

Extract leftmost characters until a space using PHP

Download Source Code

We have implemented this using a database for fetching records, If you want to extract leftmost characters until a space using a single string, you may take a look:

<?php

$myString = "Sai Sandeep";
$myString = substr($myString, 0, strpos($myString, ' '));

echo $myString;
?>

The above code prints “Sai”.

If you have a better solution, do let us know by commenting here.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: