Remove identical succesive characters from string PHP - 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 ;

Related

PHP showing last string value

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

How to replace one left character from a string?

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

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

Uppercase for first letter with php

How can I convert to uppercase for the following example :
title-title-title
Result should be:
Title-Title-Title
I tried with ucwords but it converts like this: Title-title-title
I currently have this:
echo $title = ($this->session->userdata('head_title') != '' ? $this->session->userdata('head_title'):'Our Home Page');
In this particular string example, you could explode the strings first, use that function ucfirst() and apply to all exploded strings, then put them back together again:
$string = 'title-title-title';
$strings = implode('-', array_map('ucfirst', explode('-', $string)));
echo $strings;
Should be fairly straightforward on applying this:
$title = '';
if($this->session->userdata('head_title') != '') {
$raw_title = $this->session->userdata('head_title'); // title-title-title
$title = implode('-', array_map('ucfirst', explode('-', $raw_title)));
} else {
$title = 'Our Home Page';
}
echo $title;
echo str_replace(" ","-",ucwords(str_replace("-"," ","title-title-title")));
Fiddle
Output:
Title-Title-Title
Demo
Not as swift as Ghost's but a touch more readable for beginners to see what's happening.
//break words on delimiter
$arr = explode("-", $string);
//capitalize first word only
$ord = array_map('ucfirst', $arr);
//rebuild the string
echo implode("-", $ord);
The array_map() applies callback to the elements of the given array. Internally, it traverses through the elements in our word-filled array $arr and applies the function ucfirst() to each of them. Saves you couple of lines.
Edit #2
This isn't working for the new information added to op, as there is an answer this won't be updated to reflect that.
Edit #1
$var = "title-title-title";
$var = str_replace (" ", "_", ucwords (str_replace (" ", "_", $var));
Old, non-working
$var = "title-title-title";
$var = implode("-", ucwords (explode("-", $var)));
try the following:
$str='title-title-title';
$s='';
foreach(explode('-',$str) as $si){
$s.= ($s ? "-":"").ucfirst($si);
}
$s should be Title-Title-Title at this point

PHP preg_replace

*strong text*i have a string like that "x", "x,y" , "x,y,h"
i want to user preg replace to remove the commas inside the double qutations and return the string as
"x", "xy" , "xyh"
You can just use a regular replace.
$mystring = str_replace(",", "", $mystring);
You dont need preg_replace() here and whereever possible you should trying to avoid it
$string = str_replace(',', '', $string);
This would work fine: http://codepad.org/lq7I5wkd
<?php
$myStr = '"x", "x,y" , "x,y,h"';
$chunks = preg_split("/\"[\s]*[,][\s]*\"/", $myStr);
for($i=0;$i<count($chunks);$i++)
$chunks[$i] = str_replace(",","",$chunks[$i]);
echo implode('","',$chunks);
?>
I use the following, which I've found is generally faster than regexp for this type of replacement
$string = '"x", "x,y" , "x,y,h"';
$temp = explode('"',$string);
$i = true;
foreach($temp as &$value) {
// Only replace in alternating array entries, because these are the entries inside the quotes
if ($i = !$i) {
$value = str_replace(',', '', $value);
}
}
unset($value);
// Then rebuild the original string
$string = implode('"',$temp);

Categories