I wanted to shorten long words and add '...' so it would turn :
'thisisareallylongnamedfile.jpg'
and it shall appear like 'thisisareally...'
function short_name($str)
{
$limit = 35;
if(strlen($str > $limit))
{ #
# If the name is greater than 35 characters
# then only display 35 charatcers followed
# by ...
#
return substr($str, 0, $limit) . " ...";
}
else
{ #
# Too small; so no need to change
#
return $str;
}
}
substr
(PHP 4, PHP 5)
substr — 文字列の一部分を返す
説明
string substr ( string $string, int $start [, int $length] )substr() は文字列 string の、start で指定された 位置から length バイト分の文字列を 返します。
start が正の場合、返される文字列は、 string の 0 から数えて start番目から始まる文字列となります。 例えば、文字列'abcdef'において位置 0にある文字は、'a'であり、 位置2には'c'があります。
例 2423. substr() の基本的な使用法
<?php
echo substr('abcdef', 1); // bcdef を返す
echo substr('abcdef', 1, 3); // bcd を返す
echo substr('abcdef', 0, 4); // abcd を返す
echo substr('abcdef', 0, 8); // abcdef を返す
echo substr('abcdef', -1, 1); // f
// 文字列中の 1 文字にアクセスすることも
// "中括弧" を使用することで可能
$string = 'abcdef';
echo $string{0}; // a を返す
echo $string{3}; // d を返す
echo $string{strlen($string)-1}; // f を返す
?>
start が負の場合、返される文字列は、 stringの後ろから数えて start番目から始まる文字列となります。
例 2424. 負のstartを使用
<?php
$rest = substr("abcdef", -1); // "f" を返す
$rest = substr("abcdef", -2); // "ef" を返す
$rest = substr("abcdef", -3, 1); // "d" を返す
?>
length が指定され、かつ正である場合、 返される文字列は start (string の長さに依存します) から数えてlength文字数分となります。 もし string が start の文字列長より小さいもしくは等しい場合、FALSE が返されます。
length が指定され、かつ負である場合、 string の終端から多くの文字が省略されます (start が負の場合、 開始位置は文字列の終端を過ぎているので) 。 もし start が切り出し位置を超える場合、 空文字が返されます。
例 2425. 負のlengthを使用
$rest = substr("abcdef", 0, -1); // "abcde"を返す
$rest = substr("abcdef", 2, -1); // "cde"を返す
$rest = substr("abcdef", 4, -4); // ""を返す
$rest = substr("abcdef", -3, -1); // "de"を返す
strrchr()、 substr_replace()、 preg_match()、 trim()、 mb_substr() および wordwrap() も参照ください。
substr
31-Aug-2007 11:51
31-Aug-2007 07:56
I wanted to work out the fastest way to get the first few characters from a string, so I ran the following experiment to compare substr, direct string access and strstr:
<?php
/* substr access */
beginTimer();
for ($i = 0; $i < 1500000; $i++){
$opening = substr($string,0,11);
if ($opening == 'Lorem ipsum'){
true;
}else{
false;
}
}
$endtime1 = endTimer();
/* direct access */
beginTimer();
for ($i = 0; $i < 1500000; $i++){
if ($string[0] == 'L' && $string[1] == 'o' && $string[2] == 'r' && $string[3] == 'e' && $string[4] == 'm' && $string[5] == ' ' && $string[6] == 'i' && $string[7] == 'p' && $string[8] == 's' && $string[9] == 'u' && $string[10] == 'm'){
true;
}else{
false;
}
}
$endtime2 = endTimer();
/* strstr access */
beginTimer();
for ($i = 0; $i < 1500000; $i++){
$opening = strstr($string,'Lorem ipsum');
if ($opening == true){
true;
}else{
false;
}
}
$endtime3 = endTimer();
echo $endtime1."\r\n".$endtime2."\r\n".$endtime3;
?>
The string was 6 paragraphs of Lorem Ipsum, and I was trying match the first two words. The experiment was run 3 times and averaged. The results were:
(substr) 3.24
(direct access) 11.49
(strstr) 4.96
(With standard deviations 0.01, 0.02 and 0.04)
THEREFORE substr is the fastest of the three methods for getting the first few letters of a string.
31-Jul-2007 07:06
If you need to divide a large string (binary data for example) into segments, a much quicker way to do it is to use streams and the php://memory stream wrapper.
For example, if you have a large string in memory, write it to a memory stream like
<?php
$segment_length = 8192; // this is how long our peice will be
$fp = fopen("php://memory", 'r+'); // create a handle to a memory stream resource
fputs($fp, $payload); // write data to the stream
$total_length=ftell($fp); // get the length of the stream
$payload_chunk = fread ( $fp, $segment_length );
?>
Working with large data sets, mine was 21MB, increased the speed several factors.
27-Jun-2007 06:40
All the references to "curly braces" on this page appear to be obsolete.
According to http://us.php.net/manual/en/language.types.string.php
"Using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6."
Robert Chapin
Chapin Information Services
26-Jun-2007 07:31
Starting from version 5.2.3 if $start is negative and larger then the length of the string, the result is an empty string, while in earlier versions the result was the string itself!
substr ("abcdef", -1000);
result in 5.2.0
'abcdef'
result in 5.2.3
''
This is a small inconsistency, one of those things that makes the life of a PHP programmer like hell.
18-Jun-2007 08:35
In response to the charAt functions:
It's as simple as this.
<?php
$str{$pos};
$str[$pos];
?>
http://php.net/manual/en/language.types.string.php
grep 'String access and modification by character'
07-Jun-2007 03:48
Reguarding: felipe at spdata dot com dot 29-Nov-2005 10:48
--------------------------------------------------------
JavaScript charAt PHP equivalent
<?php
function charAt($str, $pos)
{
return (substr($str, $pos, 1)) ? substr($str, $pos, 1) : -1;
}
?>
--------------------------------------------------------
There is a bug with the above code, if the charAt(..) is 0 it will return -1, because the conditional returns false.
Code below with minor fix works:
<?php
function charAt($str, $pos)
{
return (strlen(substr($str, $pos, 1))) ? substr($str, $pos, 1) : -1;
}
?>
07-Jun-2007 08:30
In PHP 5.2.2 (and probably 4.4.x) the behaviour of substr has changed for negative start index larger than the string length - now returning false in stead of the string itself. Read http://bugs.php.net/bug.php?id=41375
03-Jun-2007 08:42
"substr" seems to be implemented in a sequential way, wasting processing power proportional to the length of the string to be worked on and/or the position of the substring to be extracted.
I found a speed increase by a factor of 1000 for string extraction when the original string has a size of about 1 MB, when i replace "substr" by direct access to the chars at their index.
"Substr" should not be used for extraction of very small pieces.
25-May-2007 11:08
Get the page filename for your scripts in a easy simple way:
$pagename = substr(__FILE__, strrpos(__FILE__, "\\") + 1, strlen(__FILE__));
21-May-2007 03:15
/*####################################
'A good thing is working with added values like .NET or C#
'Here is a full copy/paste
*/###################################
#
<?php
# Written by miskotes @ 20th fo May 2007
$filenameToUpper = "Hi_Iam_toto_thomas_etc1_etc2_etc3";
$filenameToUpper = substr($filenameToUpper, 0, 19);
$toFind = "_";
$filenameToUpper = strrev($filenameToUpper); # The first time
$result = strchr($filenameToUpper, $toFind);
$result = strrev($result); # The second time
$result = substr($result, 0, -1); # Take off last found $toFind = "_"
# Don't be confused here the ultimate zero as 1st is not needed ;)
echo $result; # This displays trimmed string as word before 19th character if $toFind = True
# Else by default is previous found "_"!
?>
#
# End
11-May-2007 01:08
The functions submitted below are a waste of time and memory. To convert a string to an integer or a trimmed float, use the built in conversion instead of parsing the string, e.g :
<?php
$x = "27.2400";
echo (float)$x; // 27.24
echo (int)$x; // 27
?>
10-May-2007 06:31
Very nice a dot vanderkolk at gmail dot com, just what I needed.
I have taken your function and added a bit to it, it will strip out ALL the chars after the dot ("."). It would be useful if someone could post how to only strip if they are zeros, and round to 2 chars after the "." if they are not.
// Remove chars after . (200.000 => 200)
function removeDotZero($var)
{
$var = trim($var);
$pos = strpos($var,".");
if($pos != false)
{
$size = strlen($var);
for($i = $size; $i > $pos; $i--)
{
if(substr($var,-1,1) == "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")
$var = substr($var,0,($i - 1));
else if(substr($var,-1,1) == ".")
{
$var = substr($var,0,($i - 1));
break;
}
else
break;
}
return $var;
}
else
return $var;
}
/*-------------------*/
// Example
$str = 200.000
$str = removeDotZero($str);
echo $str;
// Output
200
29-Mar-2007 05:52
I made a function that removes the last zero's in a string or integer.
<?php
function removeDotZero($var)
{
$var = trim($var);
$pos = strpos($var,".");
if($pos != false)
{
$size = strlen($var);
for($i = $size; $i > $pos; $i--)
{
if(substr($var,-1,1) == "0")
$var = substr($var,0,($i - 1));
else if(substr($var,-1,1) == ".")
{
$var = substr($var,0,($i - 1));
break;
}
else
break;
}
return $var;
}
else
return $var;
}
echo removeDotZero("100.0052520")."\n";
echo removeDotZero("100.0000000")."\n";
?>
// produces:
// 100.005252
// 100
07-Mar-2007 06:51
A further addition to Jean-Felix function to extract data between delimeters.
The previous function wouldn't return the correct data if the delimeters used where long than one char. Instead the following function should do the job.
<?php
function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {
$posLeft = stripos($inputstr,$delimeterLeft)+strlen($delimeterLeft);
$posRight = stripos($inputstr,$delimeterRight,$posLeft+1);
return substr($inputstr,$posLeft,$posRight-$posLeft);
}
?>
01-Mar-2007 12:10
If you need to extract information in a string between delimeters then you can use this:
Inputstring is:
"Heidi Klum Supermodel" <info@HeidiKlum.com>
Here the script
<?php
$emailadresse = "\"Heidi Klum Supermodel\" <info@HeidiKlum.com>";
$outputvalue = extractBetweenDelimeters($emailadresse,"\"","\"");
echo $outputvalue; // shows Heidi Klum Supermodel
echo "<br>";
$outputvalue = extractBetweenDelimeters($emailadresse,"<",">");
echo $outputvalue; // shows info@HeidiKlum.com
function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {
$posLeft = stripos($inputstr,$delimeterLeft)+1;
$posRight = stripos($inputstr,$delimeterRight,$posLeft+1);
return substr($inputstr,$posLeft,$posRight-$posLeft);
}
?>
14-Feb-2007 07:20
/*
An advanced substr but without breaking words in the middle.
Comes in 3 flavours, one gets up to length chars as a maximum, the other with length chars as a minimum up to the next word, and the other considers removing final dots, commas and etcteteras for the sake of beauty (hahaha).
This functions were posted by me some years ago, in the middle of the ages I had to use them in some corporations incorporated, with the luck to find them in some php not up to date mirrors. These mirrors are rarely being more not up to date till the end of the world... Well, may be am I the only person that finds usef not t bre word in th middl?
Than! (ks)
This is the calling syntax:
snippet(phrase,[max length],[phrase tail])
snippetgreedy(phrase,[max length before next space],[phrase tail])
*/
function snippet($text,$length=64,$tail="...") {
$text = trim($text);
$txtl = strlen($text);
if($txtl > $length) {
for($i=1;$text[$length-$i]!=" ";$i++) {
if($i == $length) {
return substr($text,0,$length) . $tail;
}
}
$text = substr($text,0,$length-$i+1) . $tail;
}
return $text;
}
// It behaves greedy, gets length characters ore goes for more
function snippetgreedy($text,$length=64,$tail="...") {
$text = trim($text);
if(strlen($text) > $length) {
for($i=0;$text[$length+$i]!=" ";$i++) {
if(!$text[$length+$i]) {
return $text;
}
}
$text = substr($text,0,$length+$i) . $tail;
}
return $text;
}
// The same as the snippet but removing latest low punctuation chars,
// if they exist (dots and commas). It performs a later suffixal trim of spaces
function snippetwop($text,$length=64,$tail="...") {
$text = trim($text);
$txtl = strlen($text);
if($txtl > $length) {
for($i=1;$text[$length-$i]!=" ";$i++) {
if($i == $length) {
return substr($text,0,$length) . $tail;
}
}
for(;$text[$length-$i]=="," || $text[$length-$i]=="." || $text[$length-$i]==" ";$i++) {;}
$text = substr($text,0,$length-$i+1) . $tail;
}
return $text;
}
/*
echo(snippet("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea") . "<br>");
echo(snippetwop("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea") . "<br>");
echo(snippetgreedy("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea"));
*/
14-Feb-2007 12:45
Here is also a nice (but a bit slow) alternative for colorizing an true color image:
// $colorize = hexadecimal code in String format, f.e. "10ffa2"
// $im = the image that have to be computed
$red = hexdec(substr($colorize, 0, 2));
$green = hexdec(substr($colorize, 2, 2));
$blue = hexdec(substr($colorize, 4, 2));
$lum_c = floor(($red*299 + $green*587 + $blue*144) / 1000);
for ($i = 0; $i < $lum_c; $i++)
{
$r = $red * $i / $lum_c;
$g = $green * $i / $lum_c;
$b = $blue * $i / $lum_c;
$pal[$i] = $r<<16 | $g<<8 | $b;
}
$pal[$lum_c] = $red<<16 | $green<<8 | $blue;
for ($i = $lum_c+1; $i < 255; $i++)
{
$r = $red + (255-$red) * ($i-$lum_c) / (255-$lum_c);
$g = $green + (255-$green) * ($i-$lum_c) / (255-$lum_c);
$b = $blue + (255-$blue) * ($i-$lum_c) / (255-$lum_c);
$pal[$i] = $r<<16 | $g<<8 | $b;
}
$sy = imagesy($im);
$sx = imagesx($im);
for($y=0;$y<$sy;$y++)
{
for($x=0;$x<$sx;$x++)
{
$rgba = imagecolorat($im, $x, $y);
$a = ($rgba & 0x7F000000) >> 24;
$r = ($rgba & 0xFF0000) >> 16;
$g = ($rgba & 0x00FF00) >> 8;
$b = ($rgba & 0x0000FF);
$lum = floor(($r*299+$g*587+$b*144)/1000);
imagesetpixel($im, $x, $y, $a<<24 | $pal[$lum]);
}
}
19-Oct-2006 07:19
<?php
/**
* string substrpos(string $str, mixed $start [[, mixed $end], boolean $ignore_case])
*
* If $start is a string, substrpos will return the string from the position of the first occuring $start to $end
*
* If $end is a string, substrpos will return the string from $start to the position of the first occuring $end
*
* If the first character in (string) $start or (string) $end is '-', the last occuring string will be used.
*
* If $ignore_case is true, substrpos will not care about the case.
* If $ignore_case is false (or anything that is not (boolean) true, the function will be case sensitive.
* Both of the above: only applies if either $start or $end are strings.
*
* echo substrpos('This is a string with 0123456789 numbers in it.', 5, '5');
* // Prints 'is a string with 01234';
*
* echo substrpos('This is a string with 0123456789 numbers in it.', '5', 5);
* // Prints '56789'
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-string')
* // Prints 's is a string with 0123456789 numbers in it and two '
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-STRING', true)
* // Prints 's is a string with 0123456789 numbers in it and two '
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-STRING', false)
* // Prints 's is a string with 0123456789 numbers in it and two strings.'
*
* Warnings:
* Since $start and $end both take either a string or an integer:
* If the character or string you are searching $str for is a number, pass it as a quoted string.
* If $end is (integer) 0, an empty string will be returned.
* Since this function takes negative strings ('-search_string'):
* If the string your using in $start or $end is a '-' or begins with a '-' escape it with a '\'.
* This only applies to the *first* character of $start or $end.
*/
// Define stripos() if not defined (PHP < 5).
if (!is_callable("stripos")) {
function stripos($str, $needle, $offset = 0) {
return strpos(strtolower($str), strtolower($needle), $offset);
}
}
function substrpos($str, $start, $end = false, $ignore_case = false) {
// Use variable functions
if ($ignore_case === true) {
$strpos = 'stripos'; // stripos() is included above in case it's not defined (PHP < 5).
} else {
$strpos = 'strpos';
}
// If end is false, set it to the length of $str
if ($end === false) {
$end = strlen($str);
}
// If $start is a string do what's needed to make it an integer position for substr().
if (is_string($start)) {
// If $start begins with '-' start processing until there's no more matches and use the last one found.
if ($start{0} == '-') {
// Strip off the '-'
$start = substr($start, 1);
$found = false;
$pos = 0;
while(($curr_pos = $strpos($str, $start, $pos)) !== false) {
$found = true;
$pos = $curr_pos + 1;
}
if ($found === false) {
$pos = false;
} else {
$pos -= 1;
}
} else {
// If $start begins with '\-', strip off the '\'.
if ($start{0} . $start{1} == '\-') {
$start = substr($start, 1);
}
$pos = $strpos($str, $start);
}
$start = $pos !== false ? $pos : 0;
}
// Chop the string from $start to strlen($str).
$str = substr($str, $start);
// If $end is a string, do exactly what was done to $start, above.
if (is_string($end)) {
if ($end{0} == '-') {
$end = substr($end, 1);
$found = false;
$pos = 0;
while(($curr_pos = strpos($str, $end, $pos)) !== false) {
$found = true;
$pos = $curr_pos + 1;
}
if ($found === false) {
$pos = false;
} else {
$pos -= 1;
}
} else {
if ($end{0} . $end{1} == '\-') {
$end = substr($end, 1);
}
$pos = $strpos($str, $end);
}
$end = $pos !== false ? $pos : strlen($str);
}
// Since $str has already been chopped at $start, we can pass 0 as the new $start for substr()
return substr($str, 0, $end);
}
?>
16-Oct-2006 09:47
This function can replace substr() in some situations you don't want to cut right in the middle of a word. strtrim will cut between words when it is possible choosing the closest possible final string len to return. the maxoverflow parameter lets you choose how many characters can overflow past the maxlen parameter.
<?php
function strtrim($str, $maxlen=100, $elli=NULL, $maxoverflow=15) {
global $CONF;
if (strlen($str) > $maxlen) {
if ($CONF["BODY_TRIM_METHOD_STRLEN"]) {
return substr($str, 0, $maxlen);
}
$output = NULL;
$body = explode(" ", $str);
$body_count = count($body);
$i=0;
do {
$output .= $body[$i]." ";
$thisLen = strlen($output);
$cycle = ($thisLen < $maxlen && $i < $body_count-1 && ($thisLen+strlen($body[$i+1])) < $maxlen+$maxoverflow?true:false);
$i++;
} while ($cycle);
return $output.$elli;
}
else return $str;
}
?>
17-Aug-2006 01:31
<?php
//function to get a substring between between two other substrings
function substring_between($haystack,$start,$end) {
if (strpos($haystack,$start) === false || strpos($haystack,$end) === false) {
return false;
} else {
$start_position = strpos($haystack,$start)+strlen($start);
$end_position = strpos($haystack,$end);
return substr($haystack,$start_position,$end_position-$start_position);
}
}
//use of this function to get the title of an html document
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents = htmlspecialchars($contents);
$title = substring_between($contents,'<title>','</title>');
?>
18-Mar-2006 06:17
It might be obvious to some but I took some time to figure it out that you can't call
<?php
$text = substr($text, 0, -0);
?>
and expect $text to be unchanged.
A bit of context might make the issue clearer. I'm calculating how many characters I need to chop of the end of the string and them I call substr as
<?php
$text = substr($text, 0, -$charactersToChop);
?>
Sometimes $charactersToChop is set to 0 and in this case I wanted $text to be unchanged. The problem is that in this case $text gets set to an empty string.
Why? Because -0 is the same as 0 and substr($text, 0, 0) obviously returns an empty string.
In case someone want a fix:
<?php
if ($charactersToChop) {
$text = substr($text, 0, -$charactersToChop);
}
?>
That's it.
13-Feb-2006 10:21
a function to read in a file and split the string into its individual characters and display them as images for a webcounter.
can be used anywhere you need to split a string where a seperator is not present and versions where the str_split() function is also not present.
<?php
//start counter
$filename = "counter_file.txt";
$pathtoiamges = "http://www.yoursite.com/counter/";//where is your iamges
$extension = ".gif";//what filetype are your images in
//--------------do not change below this line-------------------
$counter=file_get_contents($filename);
$counter++;
$count=$counter;
$current=0;
$visit=array("");//array to hold individual characters
//split string into individual characters
//same as str_split($str) in PHP5
while (strlen($count)>0)
{
$current++;
$visit[$current]=substr($count,0,1);//get current digit
$count=substr($count,1,strlen($count));//reduce number string to remove last stored digit
}
//display images of digits
foreach ($visit as $vis)
{
if ($vis!=""){echo "<img src=\"". $pathtoimages . $vis . .$extension . "\">";}
}
$list = fopen($filename, "w+");
fwrite($list, $counter);
fclose($list);
//end counter
?>
requires a file to store the counter and 10 images to represent the digits (0-9) if used as a counter.
04-Feb-2006 11:37
Here's a function I wrote that'll insert a string into another string with an offset.
// $insertstring - the string you want to insert
// $intostring - the string you want to insert it into
// $offset - the offset
function str_insert($insertstring, $intostring, $offset) {
$part1 = substr($intostring, 0, $offset);
$part2 = substr($intostring, $offset);
$part1 = $part1 . $insertstring;
$whole = $part1 . $part2;
return $whole;
}
11-Jan-2006 06:34
Add on to "Matias from Argentina" str_format_number function.
Just added handling of $String shorter then $Format by adding a side to start the fill and a string length to the while loop.
function str_format_number($String, $Format, $Start = 'left'){
//If we want to fill from right to left incase string is shorter then format
if ($Start == 'right') {
$String = strrev($String);
$Format = strrev($Format);
}
if($Format == '') return $String;
if($String == '') return $String;
$Result = '';
$FormatPos = 0;
$StringPos = 0;
while ((strlen($Format) - 1) >= $FormatPos && strlen($String) > $StringPos) {
//If its a number => stores it
if (is_numeric(substr($Format, $FormatPos, 1))) {
$Result .= substr($String, $StringPos, 1);
$StringPos++;
//If it is not a number => stores the caracter
} else {
$Result .= substr($Format, $FormatPos, 1);
}
//Next caracter at the mask.
$FormatPos++;
}
if ($Start == 'right') $Result = strrev($Result);
return $Result;
}
05-Jan-2006 12:22
Be careful when comparing the return value of substr to FALSE. FALSE may be returned even if the output is a valid string.
substr("0", 0); // equals "0", comparision with FALSE evaluates to true, because "0" == 0 == FALSE
14-Dec-2005 07:54
Here's a little addon to the html_substr function posted by fox.
Now it counts only chars outside of tags, and doesn't cut words.
Note: this will only work in xhtml strict/transitional due to the checking of "/>" tags and the requirement of quotations in every value of a tag. It's also only been tested with the presence of br, img, and a tags, but it should work with the presence of any tag.
<?php
function html_substr($posttext, $minimum_length = 200, $length_offset = 20, $cut_words = FALSE, $dots = TRUE) {
// $minimum_length:
// The approximate length you want the concatenated text to be
// $length_offset:
// The variation in how long the text can be in this example text
// length will be between 200 and 200-20=180 characters and the
// character where the last tag ends
// Reset tag counter & quote checker
$tag_counter = 0;
$quotes_on = FALSE;
// Check if the text is too long
if (strlen($posttext) > $minimum_length) {
// Reset the tag_counter and pass through (part of) the entire text
$c = 0;
for ($i = 0; $i < strlen($posttext); $i++) {
// Load the current character and the next one
// if the string has not arrived at the last character
$current_char = substr($posttext,$i,1);
if ($i < strlen($posttext) - 1) {
$next_char = substr($posttext,$i + 1,1);
}
else {
$next_char = "";
}
// First check if quotes are on
if (!$quotes_on) {
// Check if it's a tag
// On a "<" add 3 if it's an opening tag (like <a href...)
// or add only 1 if it's an ending tag (like </a>)
if ($current_char == '<') {
if ($next_char == '/') {
$tag_counter += 1;
}
else {
$tag_counter += 3;
}
}
// Slash signifies an ending (like </a> or ... />)
// substract 2
if ($current_char == '/' && $tag_counter <> 0) $tag_counter -= 2;
// On a ">" substract 1
if ($current_char == '>') $tag_counter -= 1;
// If quotes are encountered, start ignoring the tags
// (for directory slashes)
if ($current_char == '"') $quotes_on = TRUE;
}
else {
// IF quotes are encountered again, turn it back off
if ($current_char == '"') $quotes_on = FALSE;
}
// Count only the chars outside html tags
if($tag_counter == 2 || $tag_counter == 0){
$c++;
}
// Check if the counter has reached the minimum length yet,
// then wait for the tag_counter to become 0, and chop the string there
if ($c > $minimum_length - $length_offset && $tag_counter == 0 && ($next_char == ' ' || $cut_words == TRUE)) {
$posttext = substr($posttext,0,$i + 1);
if($dots){
$posttext .= '...';
}
return $posttext;
}
}
}
return $posttext;
}
?>
29-Nov-2005 09:48
JavaScript charAt PHP equivalent
<?php
function charAt($str, $pos)
{
return (substr($str, $pos, 1)) ? substr($str, $pos, 1) : -1;
}
?>
If found, return the charecter at the specified position, otherwise return -1
<?php
function utf8_substr($str,$from,$len){
# utf8 substr
# www.yeap.lv
return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
'$1',$str);
}
?>
26-Jul-2005 06:37
Regarding the utf8_substr function from lmak: The pattern '/./u' doesn't match newline characters. This means that the substring from 0 to the total length of the string will miss the number of characters in the end matching the number of newlines in the string. To fix this one can add the s modifier (PCRE_DOTALL) in the pattern:
<?php
function utf8_substr($str,$start)
{
preg_match_all("/./su", $str, $ar);
if(func_num_args() >= 3) {
$end = func_get_arg(2);
return join("",array_slice($ar[0],$start,$end));
} else {
return join("",array_slice($ar[0],$start));
}
}
?>
05-Jul-2005 03:23
This function shortens the string down to maximum lengt defined in $max_lengt. If the string is longer the function finds the last occurance of a space and adds three dots at the end to illustrate that it is more text. If the string is without spaces it stops at exacly max lengt, also adding three dots. If the string is shorter than max lengt it returns the string as it is. This is useful for previewing long strings.
function str_stop($string, $max_length){
if (strlen($string) > $max_length){
$string = substr($string, 0, $max_length);
$pos = strrpos($string, " ");
if($pos === false) {
return substr($string, 0, $max_length)."...";
}
return substr($string, 0, $pos)."...";
}else{
return $string;
}
}
30-Jun-2005 12:07
I needed to cut a string after x chars at a html converted utf-8 text (for example Japanese text like 嬰謰弰脰欰罏).
The problem was, the different length of the signs, so I wrote the following function to handle that.
Perhaps it helps.
<?php
function html_cutstr ($str, $len)
{
if (!preg_match('/\&#[0-9]*;.*/i', $str))
{
$rVal = strlen($str, $len);
break;
}
$chars = 0;
$start = 0;
for($i=0; $i < strlen($str); $i++)
{
if ($chars >= $len)
break;
$str_tmp = substr($str, $start, $i-$start);
if (preg_match('/\&#[0-9]*;.*/i', $str_tmp))
{
$chars++;
$start = $i;
}
}
$rVal = substr($str, 0, $start);
if (strlen($str) > $start)
$rVal .= " ...";
return $rVal;
}
?>
08-Jun-2005 12:31
If you need just a single character from the string you don't need to use substr(), just use curly braces notation:
<?php
// both lines will output the 3rd character
echo substr($my_string, 2, 1);
echo $my_string{2};
?>
curly braces syntax is faster and more readable IMHO..
07-Jun-2005 07:43
I have developed a function with a similar outcome to jay's
Checks if the last character is or isnt a space. (does it the normal way if it is)
It explodes the string into an array of seperate works, the effect is... it chops off anything after and including the last space.
<?
function limit_string($string, $charlimit)
{
if(substr($string,$charlimit-1,1) != ' ')
{
$string = substr($string,'0',$charlimit);
$array = explode(' ',$string);
array_pop($array);
$new_string = implode(' ',$array);
return $new_string.'...';
}
else
{
return substr($string,'0',$charlimit-1).'...';
}
}
?>
26-May-2005 02:11
This returns the portion of str specified by the start and length parameters..
It can performs multi-byte safe on number of characters. like mb_strcut() ...
Note:
1.Use it like this bite_str(string str, int start, int length [,byte of on string]);
2.First character's position is 0. Second character position is 1, and so on...
3.$byte is one character length of your encoding, For example: utf-8 is "3", gb2312 and big5 is "2"...you can use the function strlen() get it...
Enjoy it :) ...
--- Bleakwind
QQ:940641
http://www.weaverdream.com
PS:I'm sorry my english is too poor... :(
<?php
// String intercept By Bleakwind
// utf-8:$byte=3 | gb2312:$byte=2 | big5:$byte=2
function bite_str($string, $start, $len, $byte=3)
{
$str = "";
$count = 0;
$str_len = strlen($string);
for ($i=0; $i<$str_len; $i++) {
if (($count+1-$start)>$len) {
$str .= "...";
break;
} elseif ((ord(substr($string,$i,1)) <= 128) && ($count < $start)) {
$count++;
} elseif ((ord(substr($string,$i,1)) > 128) && ($count < $start)) {
$count = $count+2;
$i = $i+$byte-1;
} elseif ((ord(substr($string,$i,1)) <= 128) && ($count >= $start)) {
$str .= substr($string,$i,1);
$count++;
} elseif ((ord(substr($string,$i,1)) > 128) && ($count >= $start)) {
$str .= substr($string,$i,$byte);
$count = $count+2;
$i = $i+$byte-1;
}
}
return $str;
}
// Test
$str = "123456���ֽ�123456�ַ���123456��ȡ����";
for($i=0;$i<30;$i++){
echo "<br>".bite_str($str,$i,20);
}
?>
17-May-2005 03:45
Hmm ... this is a script I wrote, whitch is very similar to substr, but it isn't takes html and bbcode for counting and it takes portion of string and show avoided (html & bbcode) tags too ;]
Specially usefull for show part of serach result included html and bbcode tags
<?php
/**
* string csubstr ( string string, int start [, int length] )
*
* @author FanFataL
* @param string string
* @param int start
* @param [int length]
* @return string
*/
function csubstr($string, $start, $length=false) {
$pattern = '/(\[\w+[^\]]*?\]|\[\/\w+\]|<\w+[^>]*?>|<\/\w+>)/i';
$clean = preg_replace($pattern, chr(1), $string);
if(!$length)
$str = substr($clean, $start);
else {
$str = substr($clean, $start, $length);
$str = substr($clean, $start, $length + substr_count($str, chr(1)));
}
$pattern = str_replace(chr(1),'(.*?)',preg_quote($str));
if(preg_match('/'.$pattern.'/is', $string, $matched))
return $matched[0];
return $string;
}
?>
Using this is similar to simple substr.
Greatings ;]
...
08-May-2005 12:15
See also mb_strcut() function
http://www.php.net/manual/en/function.mb-strcut.php
04-May-2005 03:47
substr($lbar_html,0,-2); // does not work
and
substr($lbar_html, 0, -2); // works as expected
do not work the same.
22-Mar-2005 04:19
Well this is a script I wrote, what it does is chop up long words with malicious meaning into several parts. This way, a chat in a table will not get stretched anymore.
<?php
function text($string,$limit=20,$chop=10){
$text = explode(" ",$string);
while(list($key, $value) = each($text)){
$length = strlen($value);
if($length >=20){
for($i=0;$i<=$length;$i+=10){
$new .= substr($value, $i, 10);
$new .= " ";
}
$post .= $new;
}
elseif($length <=15){
$post .= $value;
}
$post .= " ";
}
return($post);
}
// for example, this would return:
$output = text("Well this text doesn't get cut up, yet thisssssssssssssssssssssssss one does.", 10, 5);
echo($output); // "Well this text doesn't get cup up, yet thiss sssss sssss sssss sssss sss one does."
?>
I hope it was useful.. :)
14-Mar-2005 02:34
To quickly trim an optional trailing slash off the end of a path name:
if (substr( $path, -1 ) == '/') $path = substr( $path, 0, -1 );
11-Mar-2005 06:02
I don't know if I didn't realize this because I'm not very smart - or if i just over looked it, but the string needs to be a string. Not an integer.
ex.
<?php
$var = 12345;
echo $var{0}; // outputs nothing at all
$var = '12345';
echo $var{0}; // out puts 1
?>
25-Feb-2005 05:55
Hello,
Here you are a function to format your
numeric strings. Enjoy it.
<?php
function str_format_number($String, $Format){
if ($Format == '') return $String;
if ($String == '') return $String;
$Result = '';
$FormatPos = 0;
$StringPos = 0;
While ((strlen($Format) - 1) >= $FormatPos){
//If its a number => stores it
if (is_numeric(substr($Format, $FormatPos, 1))){
$Result .= substr($String, $StringPos, 1);
$StringPos++;
//If it is not a number => stores the caracter
} Else {
$Result .= substr($Format, $FormatPos, 1);
}
//Next caracter at the mask.
$FormatPos++;
}
return $Result;
}
// For phone numbers at Buenos Aires, Argentina
// Example 1:
$String = "541143165500";
$Format = "+00 00 0000.000";
Echo str_format_number($String, $Format); // Returns "+54 11 4316.5500"
// Example 2:
$String = "541143165500";
$Format = "+00 00 0000.0000000";
Echo str_format_number($String, $Format); // Returns "+54 11 4316.5500"
// Example 3:
$String = "541143165500";
$Format = "+00 00 0000.000 a";
Echo str_format_number($String, $Format); // Returns "+54 11 4316.550 a"
?>
How it works explanation:
str_format_number($String, $Format)
Spects two parameters $String and $Format,
both should be strings.
$String: coulbe any kind of data type,
but it's oriented to numeric string, like
phone numbers.
$Format: should be a conjunction between
numbers (any one) and others caracters.
str_format_number takes each caracter
of $Format, if it isn't a number stores
it to be returned later, but if it is a
number takes the caracter of $String
placed in the position corresponding to
the amount of numbers in $Format so far
starting from zero.
If $Format has less numbers than $string
caracters the rest of the caracters at
the end of $String should be ignored.
If $Format has more numbers than $string
caracters the no caracter will be used,
so those will be ignored.
22-Feb-2005 11:34
A fellow coder pointed out to me that $string{-n} will no longer return the character at postion -n is. Use $string{strlen($string) - n) instead.
20-Feb-2005 06:58
Hi there here is a little function i wrote to limit the number of lines in a string, i could not find anything else like it out there
function lineLimiter ($string = "", $max_lines = 1) {
$string = ereg_replace("\n", "##", $string);
$totalLines = (substr_count($string, '##') + 1);
$string = strrev($string);
$stringLength = strlen($string);
while ($totalLines > $max_lines) {
$pos = 0;
$pos = strpos ( $string, "##") + 2;
//$pos = $pos - $stringLength;
$string = substr($string, $pos);
$totalLines--;
}
$string = strrev($string);
$string = ereg_replace("##", "\n", $string);
return $string;
}
14-Feb-2005 01:36
It is also good to use substr() to get a file extension when manipulating with files. For example:
<?php
$dir = "/full/path/to/folder";
if(is_dir($dir))
{
if($dh = opendir($dir))
{
while(($file = readdir($dh)) !== false)
{
if(substr($file,strlen($file)-4,4) == ".txt")
{
echo "Filename: $file<br/>\n";
}
}
closedir($dh);
}
else
{
echo "Cannot open directory";
}
}
else
{
echo "Cannot open directory";
}
?>
Hopefully this helps
05-Feb-2005 08:10
here are two functions to shrink a string to specified lenght , normally (stri...) or reversed (...ing)
<?php
//limit chars
function limitch($value,$lenght){
if (strlen($value) >= $lenght ){
$limited = substr($value,0,$lenght);
$limited .= "...";
}
return $limited;
}
//limit chars reversed
function limitchrev($value,$lenght){
if (strlen($value) >= $lenght ){
$start = strlen($value)- $lenght;
$limited = "...";
$limited .= substr($value,$start,$lenght);
}
return $limited;
}
?>
15-Dec-2004 08:26
Split $string after each $pos, by $space
Example: <?php spaceStr('1836254','-',3); ?>
Would return '183-625-4';
<?php
function spaceStr($string,$space,$pos)
{
$cpos=$pos;
while ($cpos<strlen($string))
{
$string=substr($string,0,$cpos).$space.substr($string,$cpos);
$cpos+=strlen($space)+$pos;
};
return $string;
}
?>
03-Nov-2004 12:38
Here are the replacement functions for substr() and strlen() I use when support for html entities is required:
<?php
function html_strlen($str) {
$chars = preg_split('/(&[^;\s]+;)|/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
return count($chars);
}
function html_substr($str, $start, $length = NULL) {
if ($length === 0) return ""; //stop wasting our time ;)
//check if we can simply use the built-in functions
if (strpos($str, '&') === false) { //No entities. Use built-in functions
if ($length === NULL)
return substr($str, $start);
else
return substr($str, $start, $length);
}
// create our array of characters and html entities
$chars = preg_split('/(&[^;\s]+;)|/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);
$html_length = count($chars);
// check if we can predict the return value and save some processing time
if (
($html_length === 0) /* input string was empty */ or
($start >= $html_length) /* $start is longer than the input string */ or
(isset($length) and ($length <= -$html_length)) /* all characters would be omitted */
)
return "";
//calculate start position
if ($start >= 0) {
$real_start = $chars[$start][1];
} else { //start'th character from the end of string
$start = max($start,-$html_length);
$real_start = $chars[$html_length+$start][1];
}
if (!isset($length)) // no $length argument passed, return all remaining characters
return substr($str, $real_start);
else if ($length > 0) { // copy $length chars
if ($start+$length >= $html_length) { // return all remaining characters
return substr($str, $real_start);
} else { //return $length characters
return substr($str, $real_start, $chars[max($start,0)+$length][1] - $real_start);
}
} else { //negative $length. Omit $length characters from end
return substr($str, $real_start, $chars[$html_length+$length][1] - $real_start);
}
}
?>
Example:
html_substr("ábla6bla", 1, 4) -> "bla6"
If you happen to find any bugs, please let me know.
18-Aug-2004 03:59
Regarding windix's function to handle UTF-8 strings: one can use the "u" modifier on the regular expression so that the pattern string is treated as UTF-8 (available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32). This way the function works for other encodings too (like Greek for example).
The modified function would read like this:
<?php
function utf8_substr($str,$start)
{
preg_match_all("/./u", $str, $ar);
if(func_num_args() >= 3) {
$end = func_get_arg(2);
return join("",array_slice($ar[0],$start,$end));
} else {
return join("",array_slice($ar[0],$start));
}
}
?>
11-Aug-2004 11:34
php equivalent of Javascript's substring:
<?php
function substring($str,$start,$end){
return substr($str,$start,($end-$start));
}
?>
11-Aug-2004 08:01
Here's a little bit of code to chop strings (with html tags) around a specified length while making sure no html tags are chopped. It also prevents chopping while a tag is still open.
Note: this will only work in xhtml strict/transitional due to the checking of "/>" tags and the requirement of quotations in every value of a tag. It's also only been tested with the presence of br, img, and a tags, but it should work with the presence of any tag.
<?php
function html_substr($posttext, $minimum_length, $length_offset) {
// The approximate length you want the concatenated text to be
$minimum_length = 200;
// The variation in how long the text can be
// in this example text length will be between 200-10=190 characters
// and the character where the last tag ends
$length_offset = 10;
// Reset tag counter & quote checker
$tag_counter = 0;
$quotes_on = FALSE;
// Check if the text is too long
if (strlen($posttext) > $minimum_length) {
// Reset the tag_counter and pass through (part of) the entire text
for ($i = 0; $i < strlen($posttext); $i++) {
// Load the current character and the next one
// if the string has not arrived at the last character
$current_char = substr($posttext,$i,1);
if ($i < strlen($posttext) - 1) {
$next_char = substr($posttext,$i + 1,1);
}
else {
$next_char = "";
}
// First check if quotes are on
if (!$quotes_on) {
// Check if it's a tag
// On a "<" add 3 if it's an opening tag (like <a href...)
// or add only 1 if it's an ending tag (like </a>)
if ($current_char == "<") {
if ($next_char == "/") {
$tag_counter++;
}
else {
$tag_counter = $tag_counter + 3;
}
}
// Slash signifies an ending (like </a> or ... />)
// substract 2
if ($current_char == "/") $tag_counter = $tag_counter - 2;
// On a ">" substract 1
if ($current_char == ">") $tag_counter--;
// If quotes are encountered, start ignoring the tags
// (for directory slashes)
if ($current_char == "\"") $quotes_on = TRUE;
}
else {
// IF quotes are encountered again, turn it back off
if ($current_char == "\"") $quotes_on = FALSE;
}
// Check if the counter has reached the minimum length yet,
// then wait for the tag_counter to become 0, and chop the string there
if ($i > $minimum_length - $length_offset && $tag_counter == 0) {
$posttext = substr($posttext,0,$i + 1) . "...";
return $posttext;
}
}
}
return $posttext;
}
16-May-2004 05:55
may be by following functions will be easyer to extract the
needed sub parts from a string:
after ('@', 'biohazard@online.ge');
returns 'online.ge'
from the first occurrence of '@'
before ('@', 'biohazard@online.ge');
returns 'biohazard'
from the first occurrence of '@'
between ('@', '.', 'biohazard@online.ge');
returns 'online'
from the first occurrence of '@'
after_last ('[', 'sin[90]*cos[180]');
returns '180]'
from the last occurrence of '['
before_last ('[', 'sin[90]*cos[180]');
returns 'sin[90]*cos['
from the last occurrence of '['
between_last ('[', ']', 'sin[90]*cos[180]');
returns '180'
from the last occurrence of '['
<?
function after ($this, $inthat)
{
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
};
function after_last ($this, $inthat)
{
if (!is_bool(strrevpos($inthat, $this)))
return substr($inthat, strrevpos($inthat, $this)+strlen($this));
};
function before ($this, $inthat)
{
return substr($inthat, 0, strpos($inthat, $this));
};
function before_last ($this, $inthat)
{
return substr($inthat, 0, strrevpos($inthat, $this));
};
function between ($this, $that, $inthat)
{
return before($that, after($this, $inthat));
};
function between_last ($this, $that, $inthat)
{
return after_last($this, before_last($that, $inthat));
};
// USES
function strrevpos($instr, $needle)
{
$rev_pos = strpos (strrev($instr), strrev($needle));
if ($rev_pos===false) return false;
else return strlen($instr) - $rev_pos - strlen($needle);
};
?>
29-Aug-2003 05:39
If 'start' is negative and greater than the length of the string, PHP seems to return the first 'length' characters of the string. For example, substr('test',-10,1) returns 't'.
If you want to substring the middle of a string with another and keep the words intact:
<?php
/**
* Reduce a string by the middle, keeps whole words together
*
* @param string $string
* @param int $max (default 50)
* @param string $replacement (default [...])
* @return string
* @author david at ethinkn dot com
* @author loic at xhtml dot ne
* @author arne dot hartherz at gmx dot net
*/
function strMiddleReduceWordSensitive ($string, $max = 50, $rep = '[...]') {
$strlen = strlen($string);
if ($strlen <= $max)
return $string;
$lengthtokeep = $max - strlen($rep);
$start = 0;
$end = 0;
if (($lengthtokeep % 2) == 0) {
$start = $lengthtokeep / 2;
$end = $start;
} else {
$start = intval($lengthtokeep / 2);
$end = $start + 1;
}
$i = $start;
$tmp_string = $string;
while ($i < $strlen) {
if ($tmp_string[$i] == ' ') {
$tmp_string = substr($tmp_string, 0, $i) . $rep;
$return = $tmp_string;
}
$i++;
}
$i = $end;
$tmp_string = strrev ($string);
while ($i < $strlen) {
if ($tmp_string[$i] == ' ') {
$tmp_string = substr($tmp_string, 0, $i);
$return .= strrev ($tmp_string);
}
$i++;
}
return $return;
return substr($string, 0, $start) . $rep . substr($string, - $end);
}
echo strMiddleReduceWordSensitive ('ABCDEEF GHIJK LLKJHKHKJHKL HGHFK sdfasdfsdafsdf sadf asdf sadf sad s', 30) . "\n";
// Returns: ABCDEEF GHIJK[...]asdf sadf sad s (33 chrs)
echo strMiddleReduceWordSensitive ('ABCDEEF GHIJK LLKJHKHKJHKL HGHFK sdfasdfsdafsdf sadf asdf sadf sad s', 30, '...') . "\n";
// Returns: ABCDEEF GHIJK...asdf sadf sad s (32 chrs)
?>