Regex function to change url to another - php

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

Related

urlencode doesn't work in PHP (localhost)

So i worked in a project that i need to encode my URL, for example in my URL i have :
$url = 'http://www.test.com/?sms=+212&text=bla bla'
urlencode($url);
$url = urlencode($url);
Also i tried rawurlencode($url); and i got the same Url without changes !!
so please if someone has any idea i will be so appreciative :)
$url = "http://www.test.com/?sms=+212&text=bla bla";
echo $url = urlencode($url);
urlencode returns a new encoded url, doesn't change the $url passing to it. try this
$encoded_url = urlencode($url);
$encoded_url is what you want
Ur url data should be string.
<?php
$url = 'http://www.test.com/?sms=+212&text=bla bla';
$encoded = urlencode($url);
echo(urlencode($url)); //simple urlencode
echo('<br>');
echo($encoded);//output with varible
echo('<br>');
$url = urlencode($url);
echo($url);
Be attentive in what place u redefined ur url var. Output of that code looks like:
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
The second one really works:
$url = "http://www.test.com/?sms=+212&text=bla bla";
$url = urlencode($url);
echo $url;
See a demonstration.
I also noticed that
$url = http://www.test.com/?sms=+212&text=bla bla
is an invalid string format. Use ' ' or " " for strings:
$url = "http://www.test.com/?sms=+212&text=bla bla";

PHP preg_replace expression to remove URL parameter

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.

how to check content of request_uri

hello i would like to know how to avoid double postings in a request_uri.
as an example:
http://www.example.com/foo/foo/...
should only be:
http://www.example.com/foo/...
for example i would like to create a check that i need for a header-function that needs to be checked before it headers. it should not header if the url-string contains double postings. so the check would be something like:
$host = $_SERVER['HTTP_HOST'];
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$urlArrayUnique = array_unique($urlArray);
$urlUnique = implode('/', $urlArrayUnique);
if (isset($_SESSION['a'])){
$var = $_SESSION['a'];
if($url !== $urlUnique){
header ('Location:'.$host.'/'.$var.'/'.$basename);
exit;
}
}
if there is someone who could help me out i really would appreciate.
thanks alot.
Try the below code, use explode and get unique and implode it into URL....
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$urlArrayUnique = array_unique($urlArray);
$urlUnique = implode('/', $urlArrayUnique);
You can get dulicate values in $urlArray too...
You can modify this below code to suit as per your need:
$host = $_SERVER['HTTP_HOST'];
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$lai = max(array_keys($urlArray));//here we get the max array index
if($urlArray[$lai-1] != $urlArray[$lai])header('Location:'.$host.'/'.$var.'/'.$basename);

How do I get the Video Id from the URL? (Vimeo)

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

PHP: Remove 'WWW' from URL inside a String

Currently I am using parse_url, however the host item of the array also includes the 'WWW' part which I do not want. How would I go about removing this?
$parse = parse_url($url);
print_r($parse);
$url = $parse['host'] . $parse['path'];
echo $url;
$url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path'];
This won't remove the www in www.com, but www.www.com results in www.com.
preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);
if you don't need a protocol prefix, leave the second parameter empty
$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];

Categories