Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a substring from string through PHP?

Tags:

php

Hi want to change the displayed username like [email protected] to only abcd. so for this i should clip the part starting from @.

I can do this very easily through variablename.substring() function in Java or C# , but I m not aware with the syntax of PHP. So help me do that .

Suppose i m having variable like .

$username = "[email protected]";
$username = some

string manipultion functiona should get called here ; so that echo $username; can results in abcd only.

like image 442
Abhi Avatar asked Apr 22 '11 05:04

Abhi


People also ask

What is substr () in PHP and how it is used?

substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above. Let us see how we can use substr() to cut a portion of the string.

How can I get specific characters in a string in PHP?

From the PHP documentation: Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42] . Think of a string as an array of characters for this purpose.

Does string contain substring PHP?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .


1 Answers

Try this:

$username = substr($username, 0, strpos($username, '@'));
like image 119
Sander Marechal Avatar answered Sep 19 '22 16:09

Sander Marechal