How to remove the first character in a php line. String: How to remove the first character of a string in PHP? Trimming the text from the end

Function substr($string, $start, $length) , where $string is a variable with text, $start is the character from which the countdown begins (0 is taken as the first character), and $length is the number of characters of the selected text.

$text = "Example and text";

$first = substr($text,0,6); // Shows 6 characters starting from 0th, the very first
echo $first; // will output "Example".

$second = substr($text,8,3); // Shows 3 characters starting from the 8th
echo $second; // will print "and"
If you do not specify the Number of characters (2nd digit) parameter, the function will display all remaining characters

$text = "Example and text";

$third = substr($text,8); // Shows ALL characters starting from the 8th
echo $third; // will print "and text"

Trimming Russian text using the mb_substr PHP function

Function mb_substr performs the same tasks as substr, but is also capable of handling multibyte encodings. Therefore, in the case of Russian text, it will be useful for you mb_substr. Works the same

$text = "Example text";

$first = mb_substr($text,0,6,"UTF-8"); // Shows 6 characters starting from 0th, the very first
// Attention! Specify the encoding of the source text! In this case utf-8
echo $first; //will display "Example"

$second = mb_substr($text,7,6,"UTF-8"); // Shows 6 characters starting from the 7th
echo $second; // will output "text"
Please note that in the case of Cyrillic (Russian text) it is better to indicate the encoding at the end of the function. The example uses the most popular option - UTF-8

Trimming the text from the end

If you want to trim the text from the end, then to do this we will indicate a negative number for the variable indicating which character the text selection begins with

$text = "Sprite and Fanta";

$first = substr($text,-6,5); // Shows 5 characters starting from the 6th from the end
echo $first; //will print "Fant"

After trimming, we also add text

After trimming the variable with text, add additional text to the end

$example = "Text text text text";

$example = substr($example,0,9); // Shows 9 characters starting from 0th, the very first
$example .= "more...";
echo $example; //will display "Text text more..."
On the contrary, you can make a recording at the beginning, and then trim the text

$text = "Uncle";

$example = "George and Michael";
$example = substr($example,0,6); // Shows 6 characters starting from 0th, the very first
$text .= $example;
echo $text; //will print "Uncle George"

Trimming text to the first space

$text = "Hello world!";
echo substr($text, 0, strpos($text, " ")); // Prints "Hello"

Ready-made PHP function for trimming text by PHP Words

function cut($string, $length)(
$string = mb_substr($string, 0, $length,"UTF-8"); // cut and work with all encodings and indicate the source encoding
$position = mb_strrpos($string, " ", "UTF-8"); // determine the position of the last space. It is by this that we separate the words.
$string = mb_substr($string, 0, $position, "UTF-8"); // Trim the variable by position
return $string;
}

$text = "The Northern War ended in 1721";
echo cut($text, 17); // will display "Northern War", without the part of the word "ended"

You can trim by words through an array...

$text = "Microsoft was founded by Bill Gates back in 1975.";

$array = explode(" ",$text); // process the string into an array
$array = array_slice($array,0,5); // select the first 5 word elements
$newtext = implode(" ",$array); // convert the array back into a string

Echo $newtext;//Will display "Microsoft was founded by Bill Gates"

$str = substr($str, 1); echo $str;

This is an applepie:)

Update

After further testing, I no longer recommend using this. This caused a problem for me when using an updated string in a MySQL query, and changing to substr fixed the problem. I thought about deleting this answer, but comments suggest it is faster, so someone might use it. You may find that trimming the updated line resolves line length issues.

Sometimes you don't need a function:

$str = "";

For example:

$str = "AHello"; $str = ""; echo $str; // "Hello"

This method modifies an existing row rather than creating another one.

Echo substr("abcdef", 1); //bcdef

Notes:

Unset($str)

won't work like that how can you not disable part of the line:

Fatal error: Cannot unset string offsets

Trims occurrences of each word in the array from the beginning to the end of the string + space and optional additional single characters according to normal trim()

"x = 1 AND b = 2" $what = " ORDER BY x DESC, b ASC, "; print_r(trim_words($what, $trim_list, ",")); // => "ORDER BY x DESC, b ASC" ?>

To remove everything: starting from a line, you can use ltrim :

$str = "::f:o:"; $str = ltrim($str, ":"); var_dump($str); //=> "f:o:"

If you like using boost, but want to use a whole string as a delimiter (instead of single characters like most previously suggested solutions), you can use boost_split_iterator.

Sample code, including a convenience template:

#include #include #include template inline void split(const std::string& str, const std::string& delim, _OutputIterator result) ( using namespace boost::algorithm; typedef split_iterator It; for(It iter=make_split_iterator(str, first_finder(delim, is_equal())); iter!=It(); ++iter) ( *(result++) = boost::copy_range (*iter); ) ) int main(int argc, char* argv) ( using namespace std; vector split; split("HelloFOOworldFOO!", "FOO", back_inserter(splitted)); // or directly to console, for example split("HelloFOOworldFOO!", "FOO", ostream_iterator (cout, "\n")); return 0; )

Programmers very often have to deal with various functions for working with strings in PHP. All the functions are presented in various reference books, but I would like to limit myself to only the main ones.

PHP function substr - get part of a string

When you need to select a passage of a given length from a string starting from a given position, the substr function comes to the rescue.In this function, separated by commas, the following are passed: text, starting position and number of characters. The last parameter is optional, and if it is missing, the result of the function will be an excerpt of the source text from the starting position to the end of the line.

PHP function strlen - find out the length of a string

This function returns the length of the original string as an integer.With this function you can check the length of the data entered by the user, or maybe something else.

PHP function trim - remove extra spaces from the edges of a string

The trim function removes any whitespace characters from the edges of a string, including the newline character. There are also functions rtrim and ltrim, which remove spaces at the end or beginning of a line.

PHP function strpos - searches a string

The strpos function searches for a substring in a string and, if successful, returns the position of the beginning of that substring. After finding the first substring, the search stops.The order of the arguments in the function, as you may have guessed, is as follows: source string, substring, starting position. The third parameter is optional, but try to remember that it is there.

PHP function strip_tags - strips HTML and PHP tags from a string

The strip_tags function returns a string stripped of html and php tags. It will be useful to you when you write a comment module on your site, so as not to leave attackers the opportunity to hack your site through the form.html And php must be removed"; echo strip_tags($text); echo strip_tags($text," ");//do not delete em tags ?> The first argument of the function is the source text, and the second is the tags that should not be deleted.

PHP function strtolower - converts a string to lowercase

PHP function strtoupper - converts a string to uppercase

 If you found this site useful, you can help develop it by placing