I’m trying to get the ID from this URL but it keeps coming back empty. Here is my code:
This is what the URLs look like on the website i'm looking on:
<b><a href="index.php?page=news&id=32662f87eb22a90d81b2362c6ff458a57643eff1"
This is my PHP
$pattern = "#<b><a href=\"index.php?page=news&id=(.*?)\"#i";
preg_match_all($pattern,$openSite,$match);
? and . are special characters. You need to add a \ before them.
$pattern = "#<b><a href=\"index\.php\?page=news&id=(.*?)\"#i";
$pattern = "#\<b\>\s*\<a\s+href\=\"index\.php\?page\=news\&\;id\=([^\"\&]*)#i";
You can use a simpler pattern: /id=(.*)$/
<?php
$openSite = '<b><a href="index.php?page=news&id=32662f87eb22a90d81b2362c6ff458a57643eff1"';
$pattern = "/id=(.*)$/";
preg_match_all($pattern,$openSite,$match);
print_r($match[1])
?>
OUTPUT
Array
(
[0] => 32662f87eb22a90d81b2362c6ff458a57643eff1"
)
Related
how can I use php to get exactly the id from google play url.
Example:
Google Play Url: https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en
I want to get the com.zing.zalo . Thank you!
Simple Way
use preg_match or get the id from the url using $_GET['id'], if
you get this from url as other answer did.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
preg_match("/[^?]+(?:\?id=([^&]+).*)?/", "$Url", $matches);
echo $matches[1]; //com.zing.zalo
Working Example here Check online
The Longest way:
Simply you can use some PHP function to get it. Lets you have the following url.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
so what you need to explode the url using ? which is only one on the url.
$arr = explode("?", $Url);
From that array you need to store only the second part cause you need query string. So take only $arr[1]. Now explode again the $arr[1] with the & sign which is divide the rest of the url i mean $arr[1].
$arr2 = explode("&", $arr[1]);
Now you are all set, use another explode function to get the com.zing.zalo from the $arr2[0].
$idval = explode("=", $arr2[0]);
Result, Just echo the second part of the $idval array.
echo $idval[1]; //com.zing.zalo
Use $_GET['id'] to get the query string value of id
<?php
echo $_GET['id'];
?>
You could do this with regex: (?:\?id=)(.*)\b (I'm sure there's a more effective regex for this, but this accomplishes what you require)
preg_match('/(?:\?id=)(.*)\b/', 'https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en', $matches);
print_r($matches);
Returns:
Array
(
[0] => ?id=com.zing.zalo&
[1] => com.zing.zalo
)
I want to extract id from an url using php preg_match..
For eg: $string = 'http://www.mysite.com/profile.php?id=1111'; I need output as '1111'
using preg_match.
I tried this :
if(preg_match('/(?:https?):\/\/www\.mysite\.com\/profile\.php\?id\=[0-9]/', $string, $match) > 0){
$id = $match[1];
}
But am getting output as 'profile.php'
Can someone help me please?
Why not use parse_url with parse_str
$url_component = parse_url($string);
parse_str($url_component['query'], $output);
echo $output['id'];
if there is only one parameter in the url you can use explode function to do that easily, like
$string = 'http://www.mysite.com/profile.php?id=1111';
$ex=explode('=',$string);
$id=$ex[1];
You may also use parse_url function of php.
Hope this help,
$pattern = '/(?:https?):\/\/www\.mysite\.com\/profile\.php\?id\=([0-9]+)/';
This should work fine!
But do this with parse_url
I need to scrap a part of a url using preg_match but I never got what I need.
Here the example:
$item = "http://example.com/0229883504/?r=2-OR1&p=1";
$item = preg_match_all("/href[^\"]+/i",$item,$matches);
print_r($matches)
I need to return this number
0229883504
I tried a lot but when I var_dump the matches array, it gives:
Array ( [0] => Array ( [0] => href= ) )
I know that the problem is within the pattern but I'm not so good in this part :)
This is the code you need:
$item = "http://example.com/0229883504/?r=2-OR1&p=1";
$item = preg_match_all("#http://.*?/(.*?)/.*#i",$item,$matches);
print_r($matches);
If you need to extract the value 0229883504, you can add these lines:
$result = $matches[1][0];
echo $result;
and it will work as you can see here: http://ideone.com/H2E9I
This will do the trick for example above.
preg_match_all("/http:\/\/example.com\/([a-zA-Z0-9]+)\//",$item,$matches)
or even better:
preg_match_all("/http:\/\/example.com\/(.+)\//",$item,$matches)
However if your domains can vary use the example of the code from Aurelio :).
hey guys,
I'm a preg_replace noob and don't understand how to solve the following case:
$youtubeurl = "((http|https)\:\/\/(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)\.youtube\.(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))";
$content = preg_replace($youtubeurl, embedCode($youtubeurl), $content);
I have a pattern that matches any youtube URL.
If this pattern is matched i want to call the function embedCode() and pass along the matched string.
How can i do this. right now i'm obviously passing along the regexp code which is of course wrong. I need to pass along the matched string.
thank you
Try preg_replace_callback.
$youtube_url_pattern = "#((http|https)\:\/\/(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)\.youtube\.(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))#";
if(preg_match($youtube_url_pattern, $content_containing_url, $content)){
embedCore($contet[index]);
}
You just have to find the index of matched url, try print_r($content); inside the if and see what is the index for the matched pattern.
You are trying to do this:
if ( preg_match( '/' . $youtubeurl . '/', $content, $match ) )
{
embedCode( $match );
}
I have the following code:
$string = '[url]http://google.com[/url]';
$bbreplace = array ('/\[url\](.+?)\[\/url\]/');
$bbreplacements = array ('\\1');
$string = preg_replace($bbreplace, $bbreplacements, $string);
print $string;
Which creates a url called http://google.com/ that points to
mydomain.com/"http://google.com/"
instead of
http://google.com/
How can I fix this? Thanks
You don't need to escape the " inside '.
$bbreplacements = array ('\\1');
(BTW, use a BBcode parser.)