How to replace one left character from a string? - php

This is my simple code:
$string = "PAAUSTRALIA" ;
$output = str_replace("A","",$string);
$output = str_replace("P","",$output);
Here output is: USTRLI
But, my desired output is AUSTRALIA.
Is there any easy way in php to do this task? Simply, I want to replace one character each time from the left side of the string for my project.

Try substr along with strpos instead of str_replace as
$string = "PAAUSTRALIA" ;
$string = substr($string,(strpos($string, 'P') > -1));
$string = substr($string,(strpos($string, 'A') > -1));
echo $string; //AUSTRALIA
Edited
Making a function will also do the same as
echo removeOne($string,'p');
function removeOne($str,$value){
return substr($str,(stripos($str,$value) > -1));
}
str_replace will find the occurrence of letter 'A' within string and replace it to ''

You have to use a function to do this task smoothly. Please have a look at my code:
<?php
function onereplace($str,$replaced_character){
$spt = str_split($str,1);
for($i=0; $i <= (count($spt) - 1) ; $i++){
if($spt[$i] == $replaced_character){
unset($spt[$i]);
break;
}
}
return implode('',$spt);
}
$string = "PAAUSTRALIA" ;
$string = onereplace($string,"P");
echo $string = onereplace($string,"A");
?>
Hope it will help you!!!

Another Option would be to use preg_replace which supports a limit-parameter:
http://php.net/manual/en/function.preg-replace.php
$string = "PAAUSTRALIA" ;
$string = preg_replace("#A#", "", $string, 1);
$string = preg_replace("#P#", "", $string, 1);

echo substr($string ,1,strlen($string));
Reference:
substr(string,start,length)

str_replace will replace all the characters matched in a string.
Try to use substr()
$string = "AAUSTRALIA" ;
$output = substr($string, 1);

Please use this function in php for your task: substr(yourstring, startindex, endindex)
Edited
substr(yourstring, 0, strlen($yourstring))
For manual refer this link:http://php.net/substr
I hope this helps you.

If you just want to remove 1 char at the time from the left then all you need is substr():
$string = "PAAUSTRALIA" ;
$string = substr($string, 1); // AAUSTRALIA
$string = substr($string, 1); // AUSTRALIA

Related

PHP - Delete part of a string

I'm new to PHP and I have a problem.
I need delete all chars since a symbol (Sorry for my bad english , i'm from argentina)
I have this text :
3,94€
And I need the text is as follows:
3,94
I tried this by multiple ways but it didn't work.
There are a few ways you can do this:
Using strpos:
$string = '3,94€';
echo substr($string, 0, strpos($string, '&'));
or using strstr:
// Requires PHP 5.3+ due to the true (before_needle) parameter
$string = '3,94€';
echo strstr($string, '&', true);
or using explode:
// Useful if you need to keep the &#8364 part for later
$string = '3,94€';
list($part_a, $part_b) = explode('&', $string);
echo $part_a;
or using reset:
$string = '3,94€';
echo reset(explode('&', $string));
The best suited in your case would be to use strpos to find the first occurrence of & in the string, and then use substr to return the string from the begining until the value returned by strpos.
Another posibility is clean the number and then round it:
<?php
//Option 1: with regular expresions:
$val = '3,94&#8364';
$res = preg_replace('/[^0-9.,]/','',$val);
var_dump($res);
//Option 2: with filter functions:
$res2 = filter_var($val, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
var_dump($res2);
//Output: 3,948364
//If you want to round it:
$res = substr($res, 0, 4);
var_dump($res);
?>
You can use regex: https://regex101.com/r/uY0kH3/1
It will work in preg_match() function.
You could use str_replace.
<?php
$string = 3,94€
$final = str_replace('&#8364' ,'', $string);
echo $final;

delete string first character with index strange behavior

If I do:
$str = "+12";
$str[0] = "-"; // -12
But when I want to remove character like:
$str[0] = '';
Dumping it outputs black clubs question mark:
�12
How this works?
Try this:
$str = "+12";
echo $str = substr($str, 1);
As said by #MarkBaker the PHP docs states Assigning empty string assigns null byte. You could use the substr for this. Use the code below
<?php
$str = "+12";
$str[0] = "-"; // -12
$str = substr($str,1);
echo $str;
?>
Hope this helps you

Remove identical succesive characters from string PHP

I have this string
$string = "000000014Y00j:7";
I want to turn it into
$string = "14Y00j:7";
I want to remove all the zeros from the start of the string, in PHP
If you want to delete any chars (not only 0) you could use
$a = "000000014Y00j:7";
if($a[0]==$a[1]){
$a = ltrim($a,$a[0]);
}
echo $a;
$str = "000000014Y00j:7";
$str = ltrim($str, '0');
echo $str ;

PHP adding the value 1 to a position in an string and replace

I want to replace a letter with another character and also add 1 value more.
I mean the values are dynamic
For example I have H9 ,i want to replace as G10 .
Similarly...
H2 as G3
H6 as G7
Is it possible to use str_replace() for this ?
I got this one which works for me:
$old_string = "H2";
$new_string = "G" . (substr($old_string, 1) + 1);
echo $new_string;
Works fine for me. This is just for one value, but i guess you can loop through an array too, just have to modify the values like this
foreach($old_values as $v) {
$new_values[] = "G" . (substr($v, 1) + 1);
}
So you could save all the values into the $new_string array
Try This
<?php
$str = "H25";
preg_match_all('!\d+!', $str, $matches);
$str = str_replace($matches['0']['0'], $matches['0']['0']+1, $str, $count);
echo $str;
?>
Of course you can use str_replace()
Use
$new = array("G10", "G3", "G7");
$old = array("H9", "H2", "H6");
$string = str_replace($old, $new, $string);
where $string is your original string.
More simpler way... (Generalized Solution)
<?php
$str='G10';
preg_match('!\d+!', $str, $digit); //<---- Match the number
$str = substr($str,0,1); //<--- Grab the first char & increment it in the second line
echo ++$str.($digit[0]+1); //"prints" H11
Demo

Removing last part of string divided by a colon

I have a string that looks a little like this, world:region:bash
It divides folder names, so i can create a path for FTP functions.
However, i need at some points to be able to remove the last part of the string, so, for example
I have this world:region:bash
I need to get this world:region
The script wont be able to know what the folder names are, so some how it needs to be able to remove the string after the last colon.
$res=substr($input,0,strrpos($input,':'));
I should probably highlight that strrpos not strpos finds last occurrence of a substring in given string
$tokens = explode(':', $string); // split string on :
array_pop($tokens); // get rid of last element
$newString = implode(':', $tokens); // wrap back
You may want to try something like this:
<?php
$variable = "world:region:bash";
$colpos = strrpos($variable, ":");
$result = substr($variable, 0, $colpos);
echo $result;
?>
Or... if you create a function using this information, you get this:
<?php
function StrRemoveLastPart($string, $delimiter)
{
$lastdelpos = strrpos($string, $delimiter);
$result = substr($string, 0, $lastdelpos);
return $result;
}
$variable = "world:region:bash";
$result = StrRemoveLastPart($variable, ":");
?>
Explode the string, and remove the last element.
If you need the string again, use implode.
$items = array_pop(explode(':', $the_path));
$shotpath = implode(':', $items);
Use regular expression /:[^:]+$/, preg_replace
$s = "world:region:bash";
$p = "/:[^:]+$/";
$r = '';
echo preg_replace($p, $r, $s);
demo
Notice how $ which means string termination, is made use of.
<?php
$string = 'world:region:bash';
$string = implode(':', explode(':', $string, -1));

Categories