Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pull first 100 characters of a string in PHP

Tags:

string

php

I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.

Is there a function that can do this easily?

For example:

$string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing."; $string2 = 100charfunction($string1); print $string2 

To get:

I am looking for a way to pull the first 100 characters from a string vari 
like image 970
JoshFinnie Avatar asked Nov 25 '08 13:11

JoshFinnie


People also ask

How can I get the first 3 characters of a string in PHP?

<? php $myStr = "HelloWordl"; echo substr($myStr,0,5); ?>

How do I get the first character of a string in PHP?

To get the first character from a string, we can use the substr() function by passing 0,1 as second and third arguments in PHP.

How do I cut a string after a specific character in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

How do I slice a string in PHP?

PHP: substr() function The substr() function used to cut a part of a string from a string, starting at a specified position. The input string. Refers to the position of the string to start cutting. A positive number : Start at the specified position in the string.


2 Answers

$small = substr($big, 0, 100); 

For String Manipulation here is a page with a lot of function that might help you in your future work.

like image 114
Patrick Desjardins Avatar answered Oct 24 '22 05:10

Patrick Desjardins


You could use substr, I guess:

$string2 = substr($string1, 0, 100); 

or mb_substr for multi-byte strings:

$string2 = mb_substr($string1, 0, 100); 

You could create a function wich uses this function and appends for instance '...' to indicate that it was shortened. (I guess there's allready a hundred similar replies when this is posted...)

like image 30
Stein G. Strindhaug Avatar answered Oct 24 '22 04:10

Stein G. Strindhaug