how can i get a changable number from a string? - php

i have an string link this:
<blog_lasts limit=2>
but this part can be change : limit=5 .
how can id get the number?
and how can i get this string from a bigger string?

This is using preg_match_all(). It isn't preferred for HTML/XML content, but without knowing if this can be loaded through a DOMDocument or just a string piece, this is the easiest way.
$str = '<blog_lasts limit=2>';
preg_match_all("/<blog_lasts limit=(.*?)>/",$str,$matches);
$num = $matches[1][0];
echo $num;

Related

How to remove the string from starting after certain character

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];

How to change integer value in preg_match PHP?

sorry if my question was stupid, please someone help me to fix this issue.
i have string like
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
this $str_value is dynamic , it will change each page. now i need to replace 9 in this string as 10. add integer 1 and replace
for example if the $str_value = "http://99.99.99.99/var/test/src/158-of-box.html/251/"
then output should be
http://99.99.99.99/var/test/src/158-of-box.html/252/
i tried to replace using preg_match but i m getting wrong please somesone help me
$str = preg_replace('/[\/\d+\/]/', '10',$str_value );
$str = preg_replace('/[\/\d+\/]/', '[\/\d+\/]+1',$str_value );
Thank's for the answer, #Calimero! You've been faster than me, but I would like to post my answer, too ;-)
Another possibilty is to fetch the integer by using a group. So you don't need to trim $matches[0] to remove the slashes.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$str = preg_replace_callback('/\/([\d+])\//', function($matches) {
return '/'.($matches[1]+1).'/';
}, $str_value);
echo $str;
You need to use a callback to increment the value, it cannot be done directly in the regular expression itself, like so :
$lnk= "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$lnk= preg_replace_callback("#/\\d+/#",function($matches){return "/".(trim($matches[0],"/")+1)."/";},$lnk); // http://99.99.99.99/var/test/src/158-of-box.html/10/
Basically, the regexp will capture a pure integer number enclosed by slashes, pass it along to the callback function which will purge the integer value, increment it, then return it for replacement with padded slashes on each side.
I'd suggest also another approach based on explode and implode instead of doing any regexp stuff. In my opinion this is more readable.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/11/";
// explode the initial value by '/'
$explodedArray = explode('/', $str_value);
// get the position of the page number
$targetIndex = count($explodedArray) - 2;
// increment the value
$explodedArray[$targetIndex]++;
// implode back the original string
$new_str_value = implode('/', $explodedArray);

Read number after specific word

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=")));
?>

getting id from a string

I have the following string whose contents will vary but structure will always be the same
Tree_Group&lang=&discussionID=1012&t=viewDiscussion
How can I extract discussionID from it? No matter how long or short it may be
E.g. 6745
preg_match('/discussionID=([^&]+)/', $your_string, $matches);
$matches[1] should contain your ID.
You can extract it with parse_str:
// strip out "Tree_Group&"
$str = str_replace("Tree_Group&", "", "Tree_Group&lang=&discussionID=1012&t=viewDiscussion");
// parse string as if it we a url query string (putting results into output)
parse_str($str, $output);
// get discussionID
$discussionID = $output["discussionID"];

php getting part of a URL into a string

This question is more of a "what is the best/easiest way to do"-type-of-question. I would like to grab just the users id from a string such as
User name
I would like to parse the string and get just the "123456" part of it.
I was thinking I could explode the string but then I would get id=123456&blahblahblah and I suppose I would have to somehow dynamically remove the trash from the end. I think this may be possible with regex but I'm fairly new to PHP and so regex is a little above me.
The function parse_str() will help here
$str = "profile.php?rdc332738&id=123456&refid=22";
parse_str($str);
echo $id; //123456
echo $refid; //22
Just grab any character from id= up to & or " (the latter accounts for the case where id is put last on the query string)
$str = 'a href="/profile.php?rdc332738&id=123456&refid=22">User name</a>';
preg_match('/id=([^&"]+)/', $str, $match);
$id = $match[1];
Regex:
.*id[=]([0-9]*).*$
if you explode the string:
$string="User name"
$string_components=explode('&','$string');
The user id part (id=123456) will be:
$user_id_part=$string_components[1];
then you could do a string replace:
$user_id=str_replace('id=','$user_id_part');
And you have the user id

Categories