1

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 ?

1
  • 1
    explode it with ShopTop and print the end element of array Commented Sep 1, 2017 at 12:04

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. I like this.
0
$catName= 'ShopTop205/12.50R15';
$result = substr($catName, 7,20);
print $result;//205/12.50R15;

Comments

0

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 

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.