I have a string: /userPosts/hemlata993/20
I want to remove this /userPosts/hemlata993/.
I checked some answers but not being able to remove the first part. How can I do that? I am using php.
$string = /userPosts/hemlata993/20
I want the output as 20 because 20 is the directory or file name that I want to get
You can do it as follows:
$p = basename(parse_url("/userPosts/hemlata993/20")['path']);
echo $p; //20
If this is what you want and the format of the string is always going to be like the one that you provided, this will work:
$string = "/userPosts/hemlata993/20";
$string_arr = (explode("/",$string));
echo $string_arr[3];
Related
I have URL of file which looks like this
movieImages/1`updateCategory.PNG
it should look like this
updateCategory.PNG
you can use like this, simple
$string = 'movieImages/1`updateCategory.PNG';
$ser = 'movieImages/1`';
$trimmed = str_replace($ser, '', $string);
echo $trimmed;
output will be updateCategory.PNG
Find the position of unwanted character and then pick up the substring after that position.
$str="movieImages/1`updateCategory.PNG";
$unwanted="`";
echo substr($str,strpos($str,$unwanted)+1);
Output
updateCategory.PNG
Fiddle
That is if the string can vary in structure and size. If the first part will always remain same you can simply remove the unwanted stuff using str_replace.
echo str_replace('movieImages/1`','',$str);
I'm trying to read a number in a link in php. But I'm not sure how to do it.
http://example.com/Productdetail.asp?SID=72&ProductID=8640
How can I read the number 8640 without reading 72 at the same time.
ProductID= <- it will always be there.
SID=X <- this one will be there sometimes and left out on other pages. and the number can change between 1-999,
Is there a way to say ProductID= "read the next 4 numbers and save in string"
Thanks
you can get this output by using explode()
$link='http://example.com/Productdetail.asp?SID=72&ProductID=8640';
$links=explode('ProductID=',$link);
echo $link[1]; //this is your number
Simple use
echo $_GET['ProductID'];//8640
You can use preg_match as
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
preg_match('/&ProductID=(\d+)?/',$str,$match);
echo $match[1];
Or you can simply use parse_str as
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
parse_str($str,$res);
echo $res['ProductID'];
You can use:
strpos to get the ProductID=
's index
substr to get the string from ProductID= until the end of the string
str_replace to replace the ProductID= part
<?php
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
echo str_replace("ProductID=", "", substr($str, strpos($str, "ProductID=")));
?>
I have ran into a problem.
I want to replace only the part in parenthesis, but the problem is if the value in parenthesis is 1 or something else in the same part ([x(xxx:x:x)xx];), it will end up replacing that also, which I need the x(value) at the end of each part.
[0(267:0:0)x1];[1(257:0:0)x1];[2(256:0:0)x1];[3(258:0:0)x1];[4(261:0:0)x1]; [5(262:0:0)x64];[6(320:0:0)x10];[7(17:0:0)x32];[8(295:0:0)x2];[9(35:0:0)x2];[10(44:1:1)x6];[11(0:0:0)x1];[12(0:0:0)x1];[13(0:0:0)x1];[14(0:0:0)x1];[15(0:0:0)x1];[16(0:0:0)x1];[17(0:0:0)x1];[18(0:0:0)x1];[19(0:0:0)x1];[20(0:0:0)x1];[21(0:0:0)x1];[22(0:0:0)x1];[23(0:0:0)x1];[24(0:0:0)x1];[25(0:0:0)x1];[26(0:0:0)x1];[27(0:0:0)x1];[28(0:0:0)x1];[29(0:0:0)x1];[30(0:0:0)x1];[31(0:0:0)x1];[32(0:0:0)x1];[33(0:0:0)x1];[34(0:0:0)x1];[35(0:0:0)x1];[103(0:0:0)x1];[102(0:0:0)x1];[101(0:0:0)x1];[100(0:0:0)x1];
I cannot get this working. If I try to replace a 1 value in the part in parenthesis, it ends up changing all of the 1's on the printed part.
Also keep in mind that the zero's in the parenthesis won't always be 0's.
Here's what I tried:
$items = $row['inventory'];
$test = str_replace('267', '<img href="../assets/images/items/iron_sword.png">', $items);
echo $items;
I only tried one line of code to see how it would work.
I'm trying to replace the part in parenthesis with an image (for example, the first one is [0(267:0:0)x1];. I want to replace (267:0:0) with an image...
If you want to replace all (*:*:*) pattern with the same image, you cant try preg_replace:
$str = "[0(267:0:0)x1];[1(257:0:0)x1];[2(256:0:0)x1];[3(258:0:0)x1];[4(261:0:0)x1];";
$res = preg_replace("/\(\d+\:\d+\:\d+\)/",'<img href="../assets/images/items/iron_sword.png">', $str);
You can see the result here: http://codepad.org/UIjGKv6n
If you want to replace different item with different image, then you need an translate map like this:
$from = array(
'(267:0:0)',
'(257:0:0)',
...
);
$to = array(
'<img href="../assets/images/items/iron_sword.png">',
'<img href="../assets/images/items/other.png">',
...
);
$res = str_replace($from, $to, $str);
And if you have other requirements, you can just modify the code as you need.
I'm having trouble with PHP text parsing
I have a txt file which has this kind of information:
sometext::sometext.0 = INTEGER: 254
What i need is to get the last value of 254 as variable in PHP.
in this txt file this last value can change from 0 to 255
"sometext::sometext.0 = INTEGER: " this part doesn't change at all.
It has a length of 36 symbols, so i need get with PHP what is after 36 symbol into variable.
Thank you.
Try using sscanf:
$string = "sometext::sometext.0 = INTEGER: 254";
sscanf($string, "sometext::sometext.0 = INTEGER: %d", $number);
echo $number;
Demo: http://codepad.org/Ash2QHvI
Perhaps substr() will do?
$text = "sometext::sometext.0 = INTEGER: 254";
print substr($text, 37);
See it in action here (adjusted to match your sample data): http://codepad.org/5Ikt3kRh
Try fgets for reading a file.
For the parsing I use split. I wrote an example here. But sscanf seems to be the better option.
Since you know the string is always going to be of that form you can use the substr() function to extract the part of the string you want. Then use intval() to make it an integer if needed.
Note: My count shows that the number you're trying to get starts at the 33rd position (which becomes 32 below, as substr is 0 based), not the 36th.
You can get what you want with:
$the_str = 'sometext::sometext.0 = INTEGER: 254';
$num = intval(substr($the_str, 32));
I am currently using the following function to grab the product code number for a filename such as "62017 THOR.jpg"
$number = (int) $value;
Leaving me with 62017
The trouble is some of these files have prefixes which need to be left in place ie "WST 62017.jpg"
So im after
WST 62017
not
62017
Could someone help me, either redo what im using or alter ?
replace all characters except the numbers from the image name and get only numbers.
$number = preg_replace("~[^0-9]~", "", $value);
If you want to capture everything before the number and the number as well, you can use:
$value = "WST 62017.jpg";
$number = preg_replace('/^(.*?\d*)\..*/',"$1",trim($value));
// $number is "WST 62017"
See it
You could do it like this:
$value = preg_replace('/^(.*\d+).*$/', '\1', $filename);
It should replace everything after the first numeric value with nothing, leaving everything in front of it in place. Note that you wont't be able to cast the number to int, then.