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=")));
?>
Related
I have a string that looks something like this:
abc-def-ghi-jkl-mno-pqr-stu-vwx-yz I'd like to get the content BEFORE the 4th dash, so effectively, I'd like to get abc-def-ghi-jkl assigned to a new string, then I'd like to get mno assigned to a different string.
How could I go about doing this? I tried using explode but that changed it to an array and I didn't want to do it that way.
Try this:
$n = 4; //nth dash
$str = 'abc-def-ghi-jkl-mno-pqr-stu-vwx-yz';
$pieces = explode('-', $str);
$part1 = implode('-', array_slice($pieces, 0, $n));
$part2 = $pieces[$n];
echo $part1; //abc-def-ghi-jkl
echo $part2; //mno
See demo
http://php.net/manual/en/function.array-slice.php
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.implode.php
Can you add your source code? I done this one before but I cant remember the exact source code I used. But I am pretty sure I used explode and you can't avoid using array.
EDIT: Mark M answer is right.
you could try using substr as another possible solution
http://php.net/manual/en/function.substr.php
If I see where you are trying to get with this you could also go onto substr_replace
I guess an alternative to explode would be to find the position of the 4th - in the string and then get a substring from the start of the string up to that character.
You can find the position using a loop with the method explained at find the second occurrence of a char in a string php and then use substr(string,0,pos) to get the substring.
$string = "abc-def-ghi-jkl-mno-pqr-stu-vwx-yz";
$pos = -1;
for($i=0;$i<4;$i++)
$pos = strpos($string, '-', $pos+1);
echo substr($string, 0, $pos);
Code isn't tested but the process is easy to understand. You start at the first character (0), find a - and on the next loop you start at that position +1. The loop repeats it for a set number of times and then you get the substring from the start to that last - you found.
I need to count the number of occurences of the substring in a string.
my code like,
$str1 = "when he arrives, he want to open the door.";
$find_str = "he";
here the string "he" is occured 4 times.
how can i find it in php?..
Use substr_count:
substr_count($str1, $find_str)
Make use of the substr_count php function.
Solution:
$str1 = "when he arrives, he want to open the door.";
$find_str = "he";
substr_count($str1, $find_str);
echo substr_count($str1, $find_str);
So this is my code: i want get agm from http://example.com?agm=12345 end substr agm to get 12
echo $_GET["agm"] if substr(["agm"], 0, -3);
You have lots of errors in your code.
echo $_GET["agm"] // you should end it with ;
if substr(["agm"], 0, -3);
You should:
Use parenthesis for a criterion, so if(...)
You should use $_GET["agm"] instead of ["agm"]
You can use substr($_GET["agm"],0,2) to take first two characters.
Please refer to manual examples of substr() function.
Do you want to get the value of agm or just the...text? If it's just the text "agm", just write it out as a string...
$str = "agm";
echo $str; // agm
Otherwise, to get the contents of "agm"
$data = $_GET['agm'];
echo $data; // 12345
If that's not what you're after, I really haven't a clue what you're asking.
I'm trying to use strlen or something like that to count multiple set of characters
in a string.
Here's the database string
xa1xb2xcxd3xe4xcxa5xb6xcxd7xe8xcxa9xb10xcxd11xe12xcxa13xb14xc
I get the string from the database and i want to count the number of
"xa" "xb" "xd" and "xe" that the string contains.
I Guess i could count first xa then xb then xd then xe with strlen and then adding the numbers but isn't there an easier way?
Please help!
Are you open to preg_match_all solution?
echo preg_match_all('/x(a|b|d|e)/', $message, $matches)
From the docs:
Returns the number of full pattern matches (which might be zero), or
FALSE if an error occurred.
Although this returns the aggregated number of xa,xb,xd and xe, not the individual ones
If you need to get individual ones, you need one more step with array_count_values:
$sums = array_count_values($matches[0]);
print_r($sums);
Try this one.
$string ="xa1xb2xcxd3xe4xcxa5xb6xcxd7xe8xcxa9xb10xcxd11xe12xcxa13xb14xc";
echo substr_count($string, 'xa')."<br>";
echo substr_count($string, 'xb')."<br>";
echo substr_count($string, 'xc')."<br>";
echo substr_count($string, 'xd')."<br>";
http://php.net/manual/en/function.substr-count.php
$text = "xa1xb2xcxd3xe4xcxa5xb6xcxd7xe8xcxa9xb10xcxd11xe12xcxa13xb14xc";
echo substr_count($text, 'xa');
echo substr_count($text, 'xb');
echo substr_count($text, 'xd');
Say I had the following:
$name= "albert slevir inshten";
For the above, I would get 3.
PHP provides a function called substr_count that counts the amount of occurrences of the passed-in substring:
$count = substr_count($str, ' '); //2
To retrieve the number of words, you'd use str_word_count:
$count = str_word_count($str); //3
to count occurances of substring, use:
substr_count()
to count number of words, use:
str_word_count()
see count_chars().
I think there are only 2 spaces in this example...
Suggesting the following:
$no = count(explode(" ",$str))-1;
You can leave the -1 behind if you want the know the number of words (like 3 in your example)
PHP has a function called substr_count(), you can use it like this:
$name= "albert slevir inshten";
echo substr_count($name, ' ');