user1651894
user1651894

Reputation: 11

PHP: Removing text between first character and a (.)

For example,

The variable $creator contains a username in the format john.smith.

How do I convert this into another variable for example $creator1 which cuts down the username into the following format: jsmith.

So I want to remove everything between the first character and the '.' and put the result into another variable called $creator1.

Upvotes: 1

Views: 391

Answers (10)

khomyakoshka
khomyakoshka

Reputation: 1279

Use this regular expression:

$var1="john.smith";
$var2=preg_replace(array("/^(.{1})(.*)(\..*)/"),array("$1$3"),$var1);
echo $var2;

http://codepad.viper-7.com/ROgj02

EDIT: If a string doesn't content "." this expression just returns the same string:

$var1="john.smith";
$var2="johnsmith";
$res1=preg_replace(array("/^(.{1})(.*)(\..*)/"),array("$1$3"),$var1);
$res2=preg_replace(array("/^(.{1})(.*)(\..*)/"),array("$1$3"),$var2);
var_dump($res1,$res2);

it returns:

string(7) "j.smith" string(9) "johnsmith"

Upvotes: 0

hsz
hsz

Reputation: 152266

Try with:

$creator = 'john.smith';
list($name, $surname) = explode('.', $creator);

$creator1 = substr($name, 0, 1) . $surname;

Also possible is to treat string as an array of characters, so:

$creator1 = $name[0] . $surname;

Edit:

To check if $creator contains dot do:

$creator = 'john.smith';
if ( strpos($creator, '.') !== false ) { // contains dot
    list($name, $surname) = explode('.', $creator);
    $creator1 = substr($name, 0, 1) . $surname;
} else { // doesn't contain dot
    $creator1 = $creator;
}

Upvotes: 2

vivek salve
vivek salve

Reputation: 991

i am using my own variable please replace it with yours

$str = "john.smith";
$str1 = explode(".",$str);
$str2 = $str1[0];
$str3 = $str1[1];
$str4 = substr($str2,0,1);
$finalString = $str4.$str3;

Upvotes: 0

Santhiya
Santhiya

Reputation: 351

Try this

$creator="john.smith"
$creator_split=explode('.',$creator);
$creator_first=str_split($creator_split[0]);
$creator1=$creator_first[0].$creator_split[1];

Upvotes: 0

Faraz Kelhini
Faraz Kelhini

Reputation: 3985

$fullname = 'john.smith';
$parts = explode('.', $fullname);
$lastname = $parts[1];  // smith
$firstname = $parts[0][0]; // j

Upvotes: 1

WhoaItsAFactorial
WhoaItsAFactorial

Reputation: 3558

This will do the trick.

$creator = "john.smith";
$pos = strpos($creator,'.');
$creator1 = substr($creator,0,1).substr($creator,$pos+1,strlen($creator)-$pos);
echo $creator1; // jsmith

Upvotes: 0

You can do it different ways, I explain you one:

$name = "jonh.smith";
$data = explode(".",$name); // This will separate the name using dot as delimiter
$name1 = $data[0]; // Has jonh
$name2 = $data[1]; // Has smith
$nick = substr($name1,0,1); // Gets the first char
$nick = $nick . $name2; // Appends smith

This is a fast-coded one, but you will get the idea.

Upvotes: 1

MetalFrog
MetalFrog

Reputation: 10533

$creator = 'john.smith';
$parts = explode('.', $creator);
$condensed = $creator{0} . $parts[1];
print $condensed;
// jsmith

Upvotes: 0

lucasvscn
lucasvscn

Reputation: 1230

You can do this way:

$username = 'jhon.smith';
$new_string = $username{0} . substr($username, strpos($username,'.')+1, strlen($username));

Upvotes: 1

Florian Bauer
Florian Bauer

Reputation: 636

$creator1 = substr($creator, 0, 1).substr($creator, strpos($creator, ".")+1);

Upvotes: 0

Related Questions