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.
Related
Format of string is as follow
$img = "/images/posts/main.jpg";
$img1 = "/images/posts/john.jpg";
I need to remove /images/posts/ and echo rest of the content.
I tried with strstr, But I found it deals with only character
echo strstr($img, '/images/posts/')
//output => /images/posts/main.jpg
if I use only single character echo strstr($img, '/') Then output is images/posts/.
So I use substr with strstr to get expected result.
echo substr(strstr($img, '/'), 14);
//output => main.jpg
In my case, I am sure it will work constantly with same result because the part images/posts/ remains same and will not change.
But is it really good or fast way to counting and trimming ? Any other fast way to cut /images/posts/ at once ?
Or replace ? is it faster ??
echo str_replace('/images/posts/','',$img);
You can do like this..
Syntax-
str_replace(find, replace, string, count)
For Eg-
str_replace('/images/posts/', '', '/images/posts/main.jpg');
it will print main.jpg
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 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 want to find the string after "-" which is exactly in 3rd position, If it doesn't find the "-" it should get the whole string.
sample data are
TT-people // get people
1V-NEWTET // get NEWTET
ZZ-YESforTHE // get YESforTHE
Computer // get Computer
T-Book // get T-Book
I tried as
$result=preg_match_all("/^[a-zA-Z0-9]2-(\s\w*)/",$data,$networkID);
echo $networkID[0][1]
please rectify my error.
[a-zA-Z0-9]2
The 2 here just matches the character “2”. Looks like you meant {2}.
To reflect your updated question, it should look like this:
$result = preg_match_all('/^(?:[a-zA-Z0-9]{2}-)?(\s\w*)/', $data, $networkID);
echo $networkID[0][1];
The ? makes the group optional.
if its a single line if text you can do:
if(substr($string, 2, 1) === "-"){
echo substr($string, 3);
}else{
echo $string;
}
I have line as 'Delux Room_room'. I want get 'Delux Room' as result. I tried following codes.
Following code give following result.
echo $row.'<br/>';
echo rtrim($row, 'room') .'<br/>';
echo rtrim($row, '_room') .'<br/>';
Result ;-
Delux Room_room
Delux Room_
Delux R
But i want Delux Room. Please help me.
echo rtrim($row, '_room') .'<br/>';
That says "remove characters from the end of the string until you get to one that isn't _, r, o or m." Which obviously isn't what you mean. From the PHP manual:
$charlist You can also specify the characters you want to strip, by means of the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
You provide a list of characters, not a string you want to remove.
You should probably use substr instead:
echo substr($row, 0, -5); // remove the last 5 characers
Another solution would be to use str_replace to replace unwanted substrings:
echo str_replace('_room', '', $row); // replace "_room" with an empty string
Note that this is less precise than the substr approach.
rtrim's second argument is a set of characters. You'll have to write your own method to remove a specific string instead:
function rtrim_str($str, $trim) {
if (substr($str, -strlen($trim)) == $trim) {
return substr($str, 0, -strlen($trim));
}
return $str; // String not present at end
}
echo rtrim_str($row, '_room');
Alternatively to #lonesomeday's solution, if your last past is of variable length:
$pos = strrpos($row, '_');
echo $substr($row, 0, $pos);
You should use str_replace instead of rtrim, like this:
echo $row.'<br/>';
echo str_replace('room', '', $row) .'<br/>';
echo str_replace('_room', '', $row) .'<br/>';