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"];
Related
I want to get sub-string from a string and the sub-string will have a certain format.
Eg :
This is my test ABC-MMS-0001
Another test for ABC-MMS-00023
I need a way to get just the sub string which is in format ABC-MMS-<anynumber>
The above example should give me:
ABC-MMS-0001
ABC-MMS-00023
Try using preg_match with the pattern \b\w+-\w+-\d+\b:
$input = "This is my test ABC-MMS-0001";
$matches = array();
preg_match("/\b\w+-\w+-\d+\b/", $input, $matches);
print_r($matches)[0];
This outputs:
ABC-MMS-0001
I've already seen some posts about it, but my text is a bit complicated,
And I can not get it to work.
Part of my page:
otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=35577\u0026sil=3\u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3D\u0026sid=151078248\u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}}
What I tried:
preg_match("/otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=(.*)/", $data[$n], $output);
echo $output[1];
What I want to present:
Just the number after uid=*
If the string you receive is reliably formatted like your posted examples, where the uid= parameter is the first query parameter after ? and is strictly a numeric string, you can use preg_match() to extract it by matching with (\d+) (match digits) because whatever follows in the next query parameter won't begin with a digit.
$str = 'otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=35577\u0026sil=3\u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3D\u0026sid=151078248\u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}}';
preg_match('/\?uid=(\d+)/', $str, $output);
echo $output[1];
// Prints "35577"
In practice I would avoid this though. The best way to handle this is to treat it as the JSON stream it is, in combination with PHP's built-in URL handling methods parse_url() and parse_str().
That solution looks like:
// Note: I made this segment a valid JSON string...
$input_json = '{"otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=35577\u0026sil=3\u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3D\u0026sid=151078248\u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}';
$decoded = json_decode($input_json, TRUE);
// Parse the URL and extract its query string
// PHP_URL_QUERY instructs it to get only the query string
// but if you ever need other segments that can be removed
$query = parse_url($decoded['otherurl'], PHP_URL_QUERY);
// Parse out the query string into array $parsed_params
$params = parse_str($query, $parsed_params);
// Get your uid.
echo $parsed_params['uid'];
// Prints 35577
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;
I am trying to create a regular expression to do the following (within a preg_replace)
$str = 'http://www.site.com&ID=1620';
$str = 'http://www.site.com';
How would I write a preg_replace to simply remove the &ID=1620 from the string (taking into account the ID could be variable string length
thanks in advance
You could use...
$str = preg_replace('/[?&;]ID=\d+/', '', $str);
I'm assuming this is meant to be a normal URL, hence the [?&;]. If that's the case, the & should be a ?.
If it's part of a larger list of GET params, you are probably better off using...
parse_str($str, $params);
unset($params['ID']);
$str = http_build_query($params);
I'm guessing that & is not allowed as a character in the ID attribute. In that case, you can use
$result = preg_replace('/&ID=[^&]+/', '', $subject);
or (possibly better, thanks to PaulP.R.O.):
$result = preg_replace('/[?&]ID=[^&]+/', '', $subject);
This will remove &ID= (the second version would also remove ?ID=) plus any amount of characters that follow until the next & or end of string. This approach makes sure that any following attributes will be left alone:
$str = 'http://www.site.com?spam=eggs&ID=1620&foo=bar';
will be changed into
$str = 'http://www.site.com?spam=eggs&foo=bar';
You can just use parse_url
(that is if the URL is of the form: http://something.com?id1=1&id2=2):
$url = parse_url($str);
echo "http://{$url['host]}";
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