$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);
Using this script, I get the output:
http://website.com/632383970568166e82391f.png
And I need to extract the ID, which is abcdef and use it in another call. How can I do that?
Use regular expressions or do this:
$url = "http://subdomain.website.com/something/folder1/folder2/folder3/folder4/folder5/20385425_632383970568166e82391f.png";
$urlParts = explode('/', $url)
$imgFileParts = explode('_', $urlParts[(count($urlParts) - 1)]);
$imgId = $imgFileParts[0];
Simple regex
$re = "/\\S+\\/([\\S]+)_/";
$str = "http://subdomain.website.com/something/folder1/folder2/folder3/folder4/folder5/20385425_632383970568166e82391f.png";
preg_match($re, $str, $matches);
Related
I created a regex to match the domain name out of the input:
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = 'http://wwe.com';
if (preg_match($pattern, $url, $matches) === 1) {
echo $matches[0];
}
It works fine for this input:
http://google.com // output:google.com
But I am not able to achieve it for these inputs: (if the user enters an extra www
http://www.google.com // output:google.com
http://www.www.google.com // output:google.com
What am I missing?
Any help on this will be appreciated
What about this?
<?php
$urls = [
'http://google.com',
'http://www.google.com',
'http://www.www.google.com',
];
foreach($urls as $url) {
$url = parse_url($url, PHP_URL_HOST);
$url = preg_replace('/^(www\.)+/', '', $url);
echo $url . "\n";
}
Output:
google.com
google.com
www.google.com
I wanted to remove all occurrences of specific pattern of a parameter from a URL using preg_expression. Also removing the last "&" if exist
The pattern looks like: make=xy ("make" is fixed; "xy" can be any two letters)
Example:
http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&
After processing preg_replace, the outcome should be:
http://example.com/index.php?c=y&do=ms&r=k&p=7
I tried using:
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?lang=..&?)/i', '', $url);
However, this did not work well because I have duplicates of make=xx in the URL (which is a case that could happen in my app).
You don't need RegEx for this:
$url = "http://example.com/index.php?ok=no&make=ae&make=as&something=no&make=gr&";
list($file, $parameters) = explode('?', $url);
parse_str($parameters, $output);
unset($output['make']); // remove the make parameter
$result = $file . '?' . http_build_query($output); // Rebuild the url
echo $result; // http://example.com/index.php?ok=no&something=no
You could try using:
$str = parse_url($url, PHP_URL_QUERY);
$query = array();
parse_str($str, $query);
var_dump($query);
This will return to you the query as an array. You could then use http_build_query() function to restore the array in a query string.
But if you want to use regexp:
$url = "index.php?make=ae&ok=no&make=ae&make=as&something=no&make=gr";
echo $url."\n";
$url = preg_replace('/\b([&|&]{0,1}make=[^&]*)\b/i','',$url);
$url = str_replace('?&','?',$url);
echo $url;
This will remove all make in the URL
with rtrim you can remove last &
$url = rtrim("http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&","&");
$url = preg_replace('~&make=([a-z\-]*)~si', '', $url);
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?make=[a-z]{2})/i', '', $url);
echo $url;
Just by using preg_replace
$x = "http://example.com/index.php?c1=y&make=yu&do1=ms&r1=k&p1=7&";
$x = preg_replace(['/(\?make=[a-z]*[&])/i', '/(\&make=[a-z]*[^(&*)])/i', '/&(?!\w)/i'], ['?','',''], $x);
echo $x;
And the result is: http://example.com/index.php?c1=y&do1=ms&r1=k&p1=7
Hope this will be helpful to you guys.
Forgive me as I am a newbie programmer. How can I assign the resulting $matches (preg_match) value, with the first character stripped, to another variable ($funded) in php? You can see what I have below:
<?php
$content = file_get_contents("https://join.app.net");
//echo $content;
preg_match_all ("/<div class=\"stat-number\">([^`]*?)<\/div>/", $content, $matches);
//testing the array $matches
//echo sprintf('<pre>%s</pre>', print_r($matches, true));
$funded = $matches[0][1];
echo substr($funded, 1);
?>
Don't parse HTML with RegEx.
The best way is to use PHP DOM:
<?php
$handle = curl_init('https://join.app.net');
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$raw = curl_exec($handle);
curl_close($handle);
$doc = new DOMDocument();
$doc->loadHTML($raw);
$elems = $doc->getElementsByTagName('div');
foreach($elems as $item) {
if($item->getAttribute('class') == 'stat-number')
if(strpos($item->textContent, '$') !== false) $funded = $item->textContent;
}
// Remove $ sign and ,
$funded = preg_replace('/[^0-9]/', '', $funded);
echo $funded;
?>
This returned 380950 at the time of posting.
I am not 100% sure but it seems like you are trying to get the dollar amount that the funding is currently ?
And the character is a dollar sign that you want to strip out ?
If that is the case why not just add the dollar sign to the regex outside the group so it isn't captured.
/<div class=\"stat-number\">\$([^`]*?)<\/div>/
Because $ means end of line in regex you must first escape it with a slash.
Im totally new to regex. I need your help.
What is the regex function for changing the below url to url listed downside?
How to use that regex function in PHP.
https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/393656_257350694313804_126044397444435_712409_344887174_s.jpg
TO
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320/393656_257350694313804_126044397444435_712409_344887174_n.jpg
OR
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480/393656_257350694313804_126044397444435_712409_344887174_n.jpg
Thanks in advance.
$size = "s320x320";
$url = preg_replace("#https://(.*)/(.*)/(.*)\_s.jpg#i", "https://$1/$2/$size/$3_n.jpg", $url);
$url = str_replace("-photos-", "-sphotos-", $url);
This code is untested, but should work. The third line, str_replace, is used to make the regex simpler. :)
preg_replace( '#^(.*?)photos(.*)(/[^/]*?)_s.jpg$#', '$1sphotos$2/s320x320$3_n.jpg', $url );
Ok, let me try to answer with some code
$url = 'https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/393656_257350694313804_126044397444435_712409_344887174_s.jpg';
$str = array('fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4', '_s');
$rep1 = array('fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320', '_n');
$rep2 = array('fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480', '_n');
//TO
$url1 = str_replace($str, $rep1, $url);
//OR
$url2 = str_replace($str, $rep2, $url);
PHP
<?php
$url = 'http://vimeo.com/25451551';
/* http://www.vimeo.com/25451551 ... www.vimeo.com/25451551 */
$url = preg_match('???', $url);
echo $url;
?>
Output
25451551
Any help with this would be appreciated. Thanks.
If video IDs are allowed to start with a 0, you may need to tune the following code a bit:
$url = 'http://vimeo.com/25451551';
sscanf(parse_url($url, PHP_URL_PATH), '/%d', $video_id);
// $video_id = int(25451551)
$url = 'http://vimeo.com/25451551/test';
$result = preg_match('/(\d+)/', $url, $matches);
if ($result) {
var_dump($matches[0]);
}