Getting value after second - in php - php

I have E-20-99 in a string i want to get last value 99 and add 1 means 100 and then wants to generate new string E-20-100.

If your string ALWAYS looks like this, you can easily break it without a regex, simply by using explode()
$string = "E-20-99";
$parts = explode('-', $string);
$last_part = $parts[2] + 1;
$parts[2] = $last_part;
$string = implode('-', $parts);
echo $string;

If the string is always E-20-XX you can use
$n = ((int)substr('E-20-99', strlen('E-20-')))+1;
echo 'E-20-' . $n;
If the string might vary a bit more you could use a regualar expressions such as:
$string = 'E-20-99';
preg_match('/(E-\d+-)(\d+)/', $string, $match);
echo $match[1] . ((int)$match[2] + 1);

$old_str = 'E-20-99';
$new_str = preg_replace_callback('/(?<=-)\d+$/', function($matches) {
return $matches[0] + 1;
}, $old_str);

Related

Php get specific word in string

I have a url. I want to parse url. I don't want to get last two value. How can I do?
$str="first-second-11.1268955-15.542383564";
As I wanted
$str="first-second";
I used this code. But I don't want to get - from last value
$arr = explode("-", $str);
for ($a = 0; $a < count($arr) - 2; $a++) {
$reqPage .= $arr[$a] . "-";
}
You can use regular expressions too.Those are patterns used to match character combinations in strings.:
W*((?i)first-second(?-i))\W*
Use the 3rd param of explode() called limit:
$str="first-second-11.1268955-15.542383564";
$arr = explode("-", $str, -2);
$reqPage = implode($arr, "-"); // contains "first-second"
Regex is the fastest way for the string manipulations. Try this.
$str="first-second-11.1268955-15.542383564";
preg_match('/^[a-z]*-{1}[a-z]*/i', $str, $matches);
$match = count($matches) > 0 ? $matches[0] : '';
echo $match;

php: parse string to get string

How would I extract the number 33 before the underscore in the following?
33_restoffilename.txt.
would something like following work?
int strPos = strpos("33_filename.txt", "_");
str num = substr ("33_filename.txt" , 0 , strPos );
there are may way to achive this:
Method 1:
$strPos = strpos("33_filename.txt", "_");
echo $num = substr("33_filename.txt" , 0 , $strPos );
Method 2:
$str = '33_filename.txt';
$str_arr = explode('_', $str);
echo $num = $str_arr[0];
If the naming convention is always number_filename.ext, you can use explode():
//Put the filename into the variable $name
$name = "33_restoffilename.txt";
//Split the name by "_"
$parts = explode("_", $name);
//Get the first part of the name from the array (position 1)
$number = $parts[0];
//Output
echo $number;
This will output
33
Use strstr().
$str = '33_filename.txt';
$num = strstr($str, '_', true);
Method - 1
$getIntegerFromBeginning = substr($string, 0, strspn($string, "0123456789"));
echo $getIntegerFromBeginning;
Method - 2
$getIntegerFromBeginning = explode("_", $string, 2);
echo $getIntegerFromBeginning[0];
Replace $string with exact string.
Use this
$string = '33_filename.txt';
$arrString = explode('_', $string);
echo $value = $arrString[0]; //Will output 33
This is really very simple we don't need these string functions in this case. Just do type casting and you will get integer number. It will be fast and easy
$pp='33_restoffilename.txt';
echo (int)$pp;
No need to make it complicated

How to get all the characters between [] using Pregmatch php

I have a string
$str = "[xyz.hlp] into asasa jkljk [xyp.htq] zff [xrt.thg]";
I want to get the character from the string and make an array of all those characters . for example for the above string provided I shuould get and array like this
$array("xyz.hlp","xyp.htq","xrt.thg");
I tried using preg_match(); something like this but it didn't work
preg_match('/\[(.*)\]/', $str , $Fdesc);
Thanks in Advance
I got the desired output but by using loop and some php string functions
<?php
$str = "[xyz.hlp] into asasa jkljk [xyp.htq] zff [xrt.thg]";
$i = 0;
while ($i != strrpos($str, "]")) {
$f_pos = strpos($str, "[", $i); // for first position
$l_pos = strpos($str, "]", $f_pos + 1); // for the last position
$value = substr($str, $f_pos, ($l_pos - $f_pos) + 1);
echo $value;
$i = $l_pos;
}
?>

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

PHP function to separate integer and string part from a given string variable

I have a string variable $nutritionalInfo, this can have values like 100gm, 10mg, 400cal, 2.6Kcal, 10percent etc... I want to parse this string and separate the value and unit part into two variables $value and $unit. Is there any php function available for this? Or how can I do this in php?
Use preg_match_all, like this
$str = "100gm";
preg_match_all('/^(\d+)(\w+)$/', $str, $matches);
var_dump($matches);
$int = $matches[1][0];
$letters = $matches[2][0];
For float value try this
$str = "100.2gm";
preg_match_all('/^(\d+|\d*\.\d+)(\w+)$/', $str, $matches);
var_dump($matches);
$int = $matches[1][0];
$letters = $matches[2][0];
Use regexp.
$str = "12Kg";
preg_match_all('/^(\d+|\d*\.\d+)(\w+)$/', $str, $matches);
echo "Value is - ".$value = $matches[1][0];
echo "\nUnit is - ".$month = $matches[2][0];
Demo
I had a similar problem but none of the answers here worked for me. The problem with the other answers is they all assume you'll always have a unit. But sometimes I would have plain numbers like "100" instead of "100kg" and the other solutions would cause the value to be "10" and the units to be "0".
Here's a better solution I somewhat took from this answer. This will separate the number from ANY non-number characters.
$str = '70%';
$values = preg_split('/(?<=[0-9])(?=[^0-9]+)/i', $str);
echo 'Value: ' . $values[0]; // Value: 70
echo '<br/>';
echo 'Units: ' . $values[1]; // Units: %

Categories