I have a string like any of the following:
$str = 'A001';
$str = 'B001';
$str = 'AB001';
$str = 'B0015';
....
$str = '001A';
I want to keep only 3 characters from the end of each string.
My code is like this:
$code = str_split($str);
$code = $code[1].$code[2].$code[3];
But it works for specific cases, but not for general ones! How I can get it for general ones?
I want to keep every 3 character from end of string
Simply Use substr
echo substr($str,-3); // Last 3 characters
Second parameter to this function is start, and according to the Manual
If start is negative, the returned string will start at the start'th character from the end of string.
Fiddle
Use sbstr()
echo substr($str,-3);//get last 3 char char
Or try:
echo $str[strlen($str)-3].$str[strlen($str)-2].$str[strlen($str)-1];
You need to use substr function.
All you need to do is to pass the string, and tell it where you cut the string off. If you want to cut the string off from end, you have to provide the value in negative.
substr($str, -3);
// The third argument is optional, which specifies the length of the returned string.
Related
I have a string /en/products/saucony-switchback-iso/416.html and I would like to replace the first 4th character /en/ with /de/.
The result should be /de/products/saucony-switchback-iso/416.html
This is what I've tried:
$href = "/en/products/saucony-switchback-iso/416.html";
$href_replace = substr_replace($href, "/de/", 0);
its only returning "/de/"?
You also need to define the length of how much you're replacing in the string, which in your case is 4 (or 3, seeing as the trailing / is present in both) characters.
$href = "/en/products/saucony-switchback-iso/416.html";
$href_replace = substr_replace($href, "/de/", 0, 4);
echo $href_replace;
If you don't define a length as in your example, it defaults to the entire length of the string http://php.net/manual/en/function.substr-replace.php
length
If given and is positive, it represents the length of the portion of
string which is to be replaced. If it is negative, it represents the
number of characters from the end of string at which to stop
replacing. If it is not given, then it will default to strlen( string
); i.e. end the replacing at the end of string
Which is why you're only being left with /de/
If you want to replace /en/, str_replace is a better function
echo str_replace("/en/", "/de/", $href);
Technically you only need to do 3 (but whatever), it's simple.
echo "/de" .substr("/en/products/saucony-switchback-iso/416.html", 3);
Output
/de/products/saucony-switchback-iso/416.html
Sandbox
I also think substr will be about as fast as you can get it.
I have a STRING $special which is formatted like £130.00 and is also an ex TAX(VAT) price.
I need to strip the first char so i can run some simple addition.
$str= substr($special, 1, 0); // Strip first char '£'
echo $str ; // Echo Value to check its worked
$endPrice = (0.20*$str)+$str ; // Work out VAT
I don't receive any value when i echo on the second line ? Also would i then need to convert the string to an integer in order to run the addition ?
Thanks
Matt
+++ UPDATE
Thanks for your help with this, I took your code and added some of my own, There are more than likely nicer ways to do this but it works :) I found out that if the price was below 1000 would look like £130.00 if the price was a larger value it would include a break. ie £1,400.22.
$str = str_replace('£', '', $price);
$str2 = str_replace(',', '', $str);
$vatprice = (0.2 * $str2) + $str2;
$display_vat_price = sprintf('%0.2f', $vatprice);
echo "£";
echo $display_vat_price ;
echo " (Inc VAT)";
Thanks again, Matt
You cannot use substr the way you are using it currently. This is because you are trying to remove the £ char, which is a two-byte unicode character, but substr() isn't unicode safe. You can either use $str = substr($string, 2), or, better, str_replace() like this:
$string = '£130.00';
$str = str_replace('£', '', $string);
echo (0.2 * $str) + $str; // 156
Original answer
I'll keep this version as it still can give some insight. The answer would be OK if £ wouldn't be a 2byte unicode character. Knowing this, you can still use it but you need to start the sub-string at offset 2 instead of 1.
Your usage of substr is wrong. It should be:
$str = substr($special, 1);
Check the documentation the third param would be the length of the sub-string. You passed 0, therefore you got an empty string. If you omit the third param it will return the sub-string starting from the index given in the first param until the end of the original string.
I will start with an example to explain better what I mean. I have the following string:
$str = "x' OR firstname LIKE '%Carla%";
$returned_str = chunk_split($str,1,".");
echo $returned_str;
This string is being returned like this:
x.'. .O.R. .f.i.r.s.t.n.a.m.e. .L.I.K.E. .'.%.C.a.r.l.a.%.
So now what I am trying to do is to reverse what I did with chunk_split().
I want to remove the dots, but only the ones that are precedent of one character, and to do this successfully, the solution shouldn't remove original dots of the string.
So this string:
My name is Fábio. I like PHP. I am Portuguese.
In the end shouldn't end like this:
My name is Fábio I like PHP I am Portuguese
You can use str_split for convert your string to an array.
Then, you recreate your string in concatenating a char on two (modulo 2 for example).
Edit: or just :
$reverse = '';
for ($i=0; $i < strlen($returned_str); $i += 2)
$reverse .= $returned_str[$i];
you can use the explode function
$x=explode('.','your string here');//it returns an array
I'm trying to trim some youtube URLs that I am reading in from a playlist. The first 3 work fine and all their URLs either end in caps or numbers but this one that ends in a lower case g is getting trimmed one character shorter than the rest.
for ($z=0; $z <= 3; $z++)
{
$ythref2 = rtrim($tubeArray["feed"]["entry"][$z]["link"][0]["href"], '&feature=youtube_gdata');
The URL is http://www.youtube.com/watch?v=CuE88oVCVjg&feature=youtube_gdata .. and it should get trimmed down to .. http://www.youtube.com/watch?v=CuE88oVCVjg but instead it is coming out as http://www.youtube.com/watch?v=CuE88oVCVj.
I think it may be the ampersand symbol but I am not sure.
The second argument to rtrim is a list of characters to remove, not a string to remove.
You might want to use str_replace, or use parse_url and parse_str to get arrays of the components of the URL and the components of the query string, like "v".
Untested example code:
$youtube_url = 'http://www.youtube.com/watch?v=CuE88oVCVjg&feature=youtube_gdata';
$url_bits = parse_url($youtube_url);
$query_string = array();
parse_str($url_bits['query'], $query_string);
$video_identifier = $query_string['v']; // "CuE88oVCVjg"
$rebuilt_url = 'http://www.youtube.com/watch?v=' . $video_identifier;
No, it's the g in the second argument. rtrim() does not remove a string from the end, it removes any characters given in the second argument. Use preg_replace() or substr() instead.
$vari = "testing 245";
$numb = 0..9;
$numb_pos = strpos($vari,$numb);
echo substr($vari,0,$numb_pos);
The $numb is numbers from 0 to 9
Where am I wrong here, all I need to echo is testing
You want to cut out the numbers from a string?
$string = preg_replace('/(\d+)/', '', 'String with 1234 numbers');
Use a regular expression to strip numeric characters from your string.
or, use a regular expression to find the first instance of one either way...
Your code won't work as-is, as it'll fail if the number if the first character in the string. (You need to check $numb_pos !== false prior to the substr.)
Irrespective, if you just want to check for the existance of a number in a string, something like the following would probably be more efficient.
$digitMatched = preg_match('/\\d/im', $vari);