Remove characters from both sides of a string without use inbuilt function trim


Remove characters from both sides of a string without use inbuilt function trim. How to do?
my code is very wrong. Can you give me advise.

private function _pyutrim($search,$string_count ){
//Post しました。

	$string_count = $_POST['string'];

	$search = $_POST['string1'];
	while($string_count[$i] != '')

	$j = 0;
	while ($search[$j] != '') {

	$total_length = $i;
	$search_length = $j;

	for ($i = 0; $i < $total_length - $search_length +1; $i++) {
		if ($string_count[$i] == $search[0]) {
			$j     = 1;
			$found = true;
			while ($j < $search_length) {
				if ($string_count[$i] != $search[$j]) {
					$found = false;

				for ($j = 0; $j < $i; $j++) {
					$search[$j] = $string_count[$j];

				for ($j = $i + $search_length; $j < $total_length; $j++) {
					$search[$j] = $string_count[$j];

				$string_count   = $search;

	echo $string_count;



Hi @ayekyiphyu,

Note: If you’re talking about .trim method, which removes white space from both ends of a string in JavaScript, then I can offer some suggestions. Other than JavaScript, I can’t help much.

Do refer the code I’ve written:

It prints:

String value is: ' HelloWorld! '
String length is: 13
Front white space count is: 1
Back white space count is: 1
Trimmed String is: 'HelloWorld!'
Trimmed String length is: 11

You can play around by adding white spaces in front or at the back or both in the string for testing variable. I’ve written the sentences in console.log to ease the reading of what is printed out.

Noted: The way I write my code is based on my basic understanding of your case and my limited knowledge, it doesn’t mean it is the best way nor the most efficient/elegant way, in fact I think my code is just basic, I did use a .substring method of JavaScript which is more common.

Your code could not work for several reasons, one of it being:

then $i++; in the while loop

$j = 0;
then $j++; in the while loop

$total_length = $i;
$search_length = $j;

for ($i = 0; $i < $total_length - $search_length +1; $i++) {

Basically, your $i has been used many times, from $i = 0; then $i++ in the while loop, then $total_length = $i; then $i = 0 in the for loop, the $total_length is equal to 0 because $total_length = $i; then $i++ again.

The code has mixed up value of $i and it’s the same for the $j

That is just how I see it. You may take a look on what I suggested. :slight_smile:

Additional: if you’re not using .trim because it’s not natively available to older browser, you can consider the alternative provided in MDN
(Disclaimer: this is for JavaScript language)

//Running the following code before any other code will create trim() if it's not natively available.

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');


Are you sure the only restriction is not using trim function? Because its very easy to resort to another function:

$rest = substr("abcdef", 1, -1);  // returns "bcde"

i mean, no difficulty at all.


Is PHP a prototypal language or is the above a JS representation?


Hi @mtf, I wasn’t sure what language @ayekyiphyu posted and the reason for not using inbuilt function trim, thus the disclaimer at the start of my reply. Is it PHP? I haven’t learn those.

Just have a light google of .trim method and realize there’s one in JavaScript, so make my attempt to write JS without .trim method which caters for removing white spaces before and after a string , using a more common .substring method.

Maybe because .trim is only available later for ECMAScript 5, not before? :thinking:


Can we be that arbitrary wherein we lose dynamic ability? The regex in @codexthon’s JS snippet sums up what we want to do; but, without a regular expression–a raw script.


oops, i read character not characters, then no, can’t do

although, we can. we can simply make a function with parameter which hold the number of characters we want to get rid of, then the code is flexible again


Then you have stumbled upon the wrong room. This is indeed PHP discussion.


:sweat_smile: :joy: :sweat_smile:, I should excuse myself then. Didn’t know how similar PHP look versus JavaScript.

Excuse me. :zipper_mouth_face:


We cannot assume there is any kind of balance or predictability between the left side and right side of the contained string. There could be 50 on the left and 500 on the right. Our method needs to drill into every character from either direction until it encounters one that is not white space.


Hi @codexthon Now I finished my project. Thanks for you thinking and give me advise.