Welcome to Server Circle. It's a friendly site and all levels of experience are welcome. Be aware that we use cookies for your login.
Server Circle - Ask questions about Servers and get answers from experts.
Beta (0.52 sec)
I need to strip the last few characters off a string in PHP

I need strip the last few characters off a string in PHP - from the eleventh character of a word I don't want it to be any longer.

Is this possible ? Thank you.
Asked by:
ecololly
128 points
 Report Abuse
 Share Page - Category: Programming - Tags: I need to strip the last few characters off a string in PHP
 Enter your response
Please use Pastie.org to paste lengthy code or to fix formatting issues with code
  • Responses in reverse (2)

You're probably looking for substr http://uk2.php.net/manual/en/function.substr.php.

Something like substr($string, 0, 11) should give you the first 11 characters, but remember to check the lenght and only do this if it's over 11 chars

if (strlen($string) > 11)

{

$string = substr($string, 0, 11);

}

Response by:
_SteveWilson ...
4290 points
This should help:

http://www.php.net/manual/en/function.substr-replace.php



$var = 'ABCDEFGH:/MNRPQR/';

/* These two examples replace all of $var with 'bob'. */

echo substr_replace($var, 'bob', 0) . "
\n";

echo substr_replace($var, 'bob', 0, strlen($var)) . "
\n";

/* Insert 'bob' right at the beginning of $var. */

echo substr_replace($var, 'bob', 0, 0) . "
\n";

/* These next two replace 'MNRPQR' in $var with 'bob'. */

echo substr_replace($var, 'bob', 10, -1) . "
\n";

echo substr_replace($var, 'bob', -7, -1) . "
\n";

/* Delete 'MNRPQR' from $var. */

echo substr_replace($var, '', 10, -1) . "
\n";

Response by:
JimmyC
945 points


  • Related Questions
About Us : Contact Us : Etiquette : Terms : CDN Failover : ShorterURL : CDN Fallback : © 2013 Server Circle