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.
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.
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.
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 .
Try this:
$username = substr($username, 0, strpos($username, '@'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With