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
Related
I was wondering if there's a combo of functions or a direct function that can count how many numbers appears in a string, without use a long-way as str_split and check every character in a loop.
From a string like:
fdsji2092mds1039m
It returns that there's 8 numbers inside.
You can use filter_var() with the FILTER_SANITIZE_NUMBER_INT constant, then check the length of the new string. The new string will contain only numbers from that string, and all other characters are filtered away.
$string = "j3987snmj3j";
$numbers = filter_var($string , FILTER_SANITIZE_NUMBER_INT);
$length = strlen($numbers); // 5
echo "There are ".$length." numbers in that string";
Note that each number will be counted individually, so 137 would return 3, as would 1m3j7.
Live demo
Other solution:
function countNumbers(string $string) {
return preg_match_all('/\d/', $string, $m);
}
You can use regular expression
Try like this:
$myString = 'Som3 Charak1ers ar3 N0mberZ h3re ;)';
$countNumbers = strlen((string)filter_var($myString, FILTER_SANITIZE_NUMBER_INT));
echo 'Your input haz ' . $countNumbers . ' digits in it, man';
You can also make a function out of this to return only the number, if you need it.
Following code does what you intend to do:
<?php
$string = 'dsfds98fsdfsdf8sdf908f9dsf809fsd809f8s15d0d';
$splits=str_split($string);
$count=0;
foreach ($splits as $split){
if(is_numeric($split)){
$count++;
}
}
print_r($count);
Output: 17
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
How can I do the following with php?
This is my example:
http://www.example.com/index.php?&xx=okok&yy=no&bb=525252
I want remove this part: &yy=no&bb=525252
I just want this result:
http://www.example.com/index.php?&xx=okok
I tried this :
$str = 'bla_string_bla_bla_bla';
echo preg_replace('/bla_/', '', $str, 1); ;
but this not what I want.
Going for preg_replace was a good start. But you need to learn about regexes.
This will work:
$str = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
echo preg_replace ('/&yy.+$/', '', $str);
Here the regex is &yy.+$
Let's see how this works:
&yy matches &yy obviously
.+ matches everything ...
$ ... until the end of the string.
So here, my replacement says : Replace whatever begins by &yy until the end of the string by nothing, which is actually simply deleting this part.
You can do this:
$a = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
$b = substr($a,0,strpos($a,'&yy')); // Set in '&yy' the string to identify the beginning of the string to remove
echo $b; // Will print http://www.example.com/index.php?&xx=okok
Are you always expecting the end part to have the 'yy' variable name? You could try this:
$str = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
$ex = explode('&yy=', $str, 2);
$firstPart = $ex[0];
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));
I have this string:
"small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg"
I want to get only the first image link (after the word "small") and ignore all the rest (the word "boxshot" and the link after it).
How can I make it?
use it and like my ans. string is your above whole string
substr(string,0,stripos(string,"boxshot")-1);
$string = "small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg";
preg_match_all('~http(.*?)jpg~i',trim($string),$matches);
var_dump($matches[0][0]);
The easy way:
<?php
$str='small: http://img.exent.com/free/frg/products/666550/player_boxshot.jpg boxshot: http://img.exent.com/free/frg/products/666550/boxshot.jpg';
$parts=explode('boxshot:',$str);
$small=$parts[0];
echo($small);
?>
Try this:
$string = "small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg";
// replace all 2 or more consecutive spaces with 1 space
$string = preg_replace( '/\s\s+/i', ' ', $string);
$array = explode( ' ', $string);
echo $array[1];
Hope this helps.