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
Related
I want to display only last string value from string. This is my string ShopTop205/12.50R15
I want to just display this type of string
205/12.50R15
I have tried like
<?php
$catName= 'ShopTop205/12.50R15';
echo substr($catName, strrpos($catName, ' ') + 1);
?>
second way
<?php
$string = 'ShopTop205/12.50R15';
$string = explode('', $string);
$last_string = end($string);
echo $last_string;
?>
I have used substr() function also but i could not get result that i want.
how could i do this ?
You may remove the initial non-numeric chars with a regex:
$catName= 'ShopTop205/12.50R15';
$res = preg_replace('~^\D+~', '', $catName);
echo $res; // => 205/12.50R15
See the PHP demo
The pattern is ^\D+ here, and it matches any one or more (+) chars other than digits (\D) at the start of the string (^).
See the regex demo.
$catName= 'ShopTop205/12.50R15';
$result = substr($catName, 7,20);
print $result;//205/12.50R15;
Check this one
$catName= 'ShopTop205/12.50R15';
preg_match('/^\D*(?=\d)/', $catName, $m);
$pos = isset($m[0]) ? strlen($m[0]) : false;
$text = substr($catName,$pos); // this will contain 205/12.50R15
Doing it with substr() given that the length is always the same:
https://ideone.com/A4Avpt
<?php
echo substr('ShopTop205/12.50R15', -12);
?>
Output: 205/12.50R15
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 € 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€';
$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('€' ,'', $string);
echo $final;
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
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 ;
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