|
|
 |
substr_replace (PHP 4, PHP 5) substr_replace -- Replace text within a portion of a string Descriptionstring substr_replace ( string string, string replacement, int start [, int length] )
substr_replace() replaces a copy of
string delimited by the
start and (optionally)
length parameters with the string given in
replacement. The result is returned.
If start is positive, the replacing will
begin at the start'th offset into
string.
If start is negative, the replacing will
begin at the start'th character from the
end of string.
If length is given and is positive, it
represents the length of the portion of
string which is to be replaced. If it is
negative, it represents the number of characters from the end of
string at which to stop replacing. If it
is not given, then it will default to strlen(
string ); i.e. end the replacing at the
end of string.
Example 1. substr_replace() example |
<?php
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var<hr />\n";
echo substr_replace($var, 'bob', 0) . "<br />\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";
echo substr_replace($var, 'bob', 0, 0) . "<br />\n";
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";
echo substr_replace($var, '', 10, -1) . "<br />\n";
?>
|
|
Note: This function is
binary-safe.
See also str_replace() and
substr().
User Contributed Notes
substr_replace
ogt at parasane dot com
21-Jan-2005 05:02
Actually, just a minor correction to tekrat at 2d dot com's post:
Change the code....
<?
if(strlen($substring) < 1){
$string = $rep;
}else{
$string = $substring;
}
?>
.... into....
<?
if(strlen($substring) >= 1){
$string = $substring;
}
?>
.... otherwise you'll end up with the elipses (...) for any null strings.
tekrat at 2d dot com
06-Jan-2005 10:55
Here's a slightly revised version of the truncation function above.
Theres isn't much of a reason to add in the $rep at the end of the original string is less then the truncation break point.
<?
function truncate($substring, $max = 50, $rep = '...') {
if(strlen($substring) < 1){
$string = $rep;
}else{
$string = $substring;
}
$leave = $max - strlen ($rep);
if(strlen($string) > $max){
return substr_replace($string, $rep, $leave);
}else{
return $string;
}
}
?>
danieldoorduin at hotmail dot com
10-Dec-2004 04:48
Using substr_replace() can be avoided by using substr() instead:
<?
$string = substr($string, 0, $position_needle).$replace.substr($string, $position_needle+$length_needle);
?>
This can be useful when you need to replace parts of multibyte strings like strings encoded with utf-8. There isn't a multibute variant for substr_replace(), but for php substr() there is mb_substr(). For more information on multibyte strings see http://nl3.php.net/manual/en/ref.mbstring.php
titbits at nospam-4logical dot co dot uk
04-Aug-2004 04:28
A simple but useful 'pluralize' function using substr_replace:
function pluralize($noun) {
if ($noun{strlen($noun) -1} == "y")
$noun = substr_replace($noun, "ies", strlen($noun) -1);
else
$noun .= "s";
return $noun;
}
Handy when displaying dynamic text.
dmron
17-Jun-2004 06:34
Regarding "...", even the short functions are too long and complicated, and there's no need to use substr_replace. substr() works better and is way faster prior to 4.3.5 as the below poster stated.
function shorten( $str, $num = 100 ) {
if( strlen( $str ) > $num ) $str = substr( $str, 0, $num ) . "...";
return $str;
}
philip
13-May-2004 02:55
The substr_replace() function is extremely slow in PHP versions prior to 4.3.5 and 5.0.0 so consider using an alternative before this time.
tony at outshine dot com
10-May-2004 04:25
The comment by geniusdex is a good one. Short, simple functions are the best. But if the string is not longer than the limit set, NOTHING is returned. Here is the function re-done to always return a string:
<?php
function dot($str, $len, $dots = "...") {
if (strlen($str) > $len) {
$dotlen = strlen($dots);
$str = substr_replace($str, $dots, $len - $dotlen);
}
return $str;
}
?>
geniusdex ( at ) brz ( dot ) nu
23-Feb-2004 09:33
This is my version of making dotted strings:
<?php
function dot($str, $len, $dots = "...") {
if (strlen($str) > $len) {
$dotlen = strlen($dots);
substr_replace($str, $dots, $len - $dotlen);
}
}
?>
Thijs Wijnmaalen (thijs[at]nllinux.nl)
20-Jan-2004 01:05
I wrote a function that you can use for example in combination with a search script to cut off the articles that are too long.
<?php
function substr_index($text, $maxChars = 20, $splitter
= '...') {
$theReturn = $text;
$lastSpace = false;
if (strlen($text) > $maxChars) {
$theReturn = substr($text, 0, $maxChars - 1);
if (in_array(substr($text, $maxChars - 1, 1),
array(' ', '.', '!', '?'))) {
$theReturn .= substr($text, $maxChars, 1);
} else {
$theReturn = substr($theReturn, 0, $maxChars -
strlen($splitter));
$lastSpace = strrpos($theReturn, ' ');
if ($lastSpace !== false) {
$theReturn = substr($theReturn, 0, $lastSpace);
}
if (in_array(substr($theReturn, -1, 1), array(','))) {
$theReturn = substr($theReturn, 0, -1);
}
$theReturn .= $splitter;
}
}
return $theReturn;
}
?>
neon at lordneon dot com
05-Nov-2003 06:40
The easiest way (I think) to add trailing dots after a string which in my case are too long is:
<?
function dots($num, $string) {
if (strlen($string) < $num) {
$string = substr_replace($string, '...', '-10', $num);
}
return $string;
}
Then on your page do something like:
<? echo dots("30" $row['title']); ?>
if the string is greater than the specific number it'll replace 3 dots.
I hope this helps =)
?>
david at ethinkn dot com
05-Jul-2003 07:36
Here is a simple function to shorten a string and add an ellipsis
<?php
function truncate ($string, $max = 50, $rep = '') {
$leave = $max - strlen ($rep);
return substr_replace($string, $rep, $leave);
}
echo truncate ('akfhslakdhglksjdgh', 10, '...');
?>
thomasNOSPAM at sportentranceNOSPAM dot com
08-Oct-2002 05:01
To abbreviate links into '...' if they outreach a certain amount of space; use the preg_replace function instead.
For instance you grabbed the headlines of a news site for use on your own page and the lines are to long:
asuming the raw material is stored in $unedited;
$edited = preg_replace("/(>)([[:print:]]{52,})(<)/e", "'\\1'.substr_replace('\\2 ', '...', '48').'\\3'", $unedited);
echo $edited;
This will shorten strings longer than 52 characters into 51 characters, with the last being three dots...
klaas at group94 dot com
13-Feb-2002 12:38
THE DOT DOT DOT ISSUE
PROBLEM:
You want to abbreviate a string.
E.g. You want "BritneySpears" to show as "BritneySpe...", being only the ten first characters followed by "..."
SOLUTION:
<?
$oRIGINAL = "BritneySpears";
$sHORTER = substr_replace($oRIGINAL, '...', 10);
echo ($sHORTER);
?>
This will result in BritneySpe...
28-Sep-2001 10:30
If you would like to remove characters from the start or end of a string, try the substr() function.
For example, to remove the last three characters from a string:
$string = "To be or not to be.";
$string = substr ($string, 0, -3);
mrbrown8 at juno dot com
16-Apr-2001 02:16
Just to add to the examples, if replacement is longer than length, only the length number of chars are removed from string and all of replacement is put in its place, and therefor strlen($string) is inreased.
$var = 'ABCDEFGH:/MNRPQR/';
/* Should return ABCDEFGH:/testingRPQR/ */
echo substr_replace ($var, 'testing', 10, 2);
stefan at maifei dot com
01-Apr-2001 03:33
If you are trying to use -0, I don't think it works. For example:
substr_replace($file,'',-4,0)
There may be an alternative though...
jgainey at infoave dot net
13-Mar-2001 08:29
[Editor's note: for a much simpler solution, use number_format()]
I had a situation in which I needed to add a comma to the third position of a number(the price of something).
<p>
$price = "12000";<br>
$price = substr_replace ($price, ',', -3, 0)";<br>
the result would be 12,000<p>
the -3 counts from right to left. a regular 3 would count from left to right
I hope this helps...
| |