How to get parameter from url split by colon (:) - php

I have this url: "http://example.com/search/dep_city:LON/arr_city=NYC".
How can i get the values "LON" and "NYC" from this url in PHP?
This would be easy if url would be in format "dep_city=LON" by using "$_GET['dep_city']" but in this case i am confused how to do this.
Thanks in advance for your time and help.

Get the URL from the server, explode it into parts.
Check the index of : or = for each part, parse it into an array.
<?php
$url = substr( $_SERVER["REQUEST_URI"], 1, strlen($_SERVER["REQUEST_URI"]));
$vars = explode("/", $url);
foreach($vars as $var){
$indexEquals = strpos($var, "=");
if($indexEquals === false){
$indexColon = strpos($var, ":");
$part1 = substr($var, 0, $indexColon);
$part2 = substr($var, $indexColon+1, strlen($var));
}else{
$part1 = substr($var, 0, $indexEquals);
$part2 = substr($var, $indexEquals+1, strlen($var));
}
$params[$part1] = $part2;
}
echo json_encode($params);
die();

Related

PHP remove extension from image url

I need to change images extension from jpg / png / gif or else to webp urls from
https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.jpg
to
https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.webp
Use preg_replace:
$newUrl = preg_replace('/(?:jpg|png|gif)$/i', 'webp', $url);
Here is the unit test for you:
class UrlReplaceTest extends \PHPUnit_Framework_TestCase
{
public function testTestUrlReplace()
{
$url = 'https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.jpg';
$url1 = 'https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.png';
$url2 = 'https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.gif';
$url3 = 'https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.webp';
$url4 = 'https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.JPG';
$replacedUrl = $this->replaceImageExtensionInUrlToWebp($url);
$replacedUrl1 = $this->replaceImageExtensionInUrlToWebp($url1);
$replacedUrl2 = $this->replaceImageExtensionInUrlToWebp($url2);
$replacedUrl3 = $this->replaceImageExtensionInUrlToWebp($url3);
$replacedUrl4 = $this->replaceImageExtensionInUrlToWebp($url4);
$expected = 'https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.webp';
$this->assertEquals($expected, $replacedUrl);
$this->assertEquals($expected, $replacedUrl1);
$this->assertEquals($expected, $replacedUrl2);
$this->assertEquals($expected, $replacedUrl3);
$this->assertEquals($expected, $replacedUrl4);
}
private function replaceImageExtensionInUrlToWebp($url)
{
return preg_replace('/(?:jpg|png|gif)$/i', 'webp', $url);
}
}
$url = "https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.jpg";
$id = substr($url, strrpos($url, '/') + 1);
$first = dirname($url);
$arr = explode(".", $id);
$last = $arr[0].".webp";
$new_url = $first.'/'.$last;
Simple strings operations:
$s = "https://res-3.cloudinary.com/543/image/upload/dpr_auto,w_800,/2342/59_M.jpg";
$s_result = substr($s, 0, 1 + strrpos($s, ".")) . "webp";
Explained:
// find position of last dot "."
$pos = strrpos( $s, "." );
// loose the extension
// get substring from start of string to the last dot including the dot ( the + 1 )
$s_s = substr($s, 0, 1 + $pos );
// add your string to the end of it
$s_s .= "webp";
This works for any extensions. I think this is the most generic solution for your problem done the way you requested - php, strings.

URL Rewrite - change URL text between slashes

I have URLs like this:
http://www.mywebsite.com/carmake/ABCDEFG/123456789
http://www.mywebsite.com/carmake/AAABBBC/124532532
http://www.mywebsite.com/carmake/BNDFKNV/463634213
and I want to change them to this:
http://www.mywebsite.com/carmake/parts/123456789
http://www.mywebsite.com/carmake/parts/124532532
http://www.mywebsite.com/carmake/parts/463634213
How can I change the text between the last to slashes to parts in functions.php
https://regex101.com/r/sH1wA1/1
<?php
$string = 'http://www.mywebsite.com/carmake/ABCDEFG/123456789';
$pattern = '/(.*\/).*\/([^\/]*$)/';
$replacement = '${1}parts/${2}';
echo preg_replace($pattern, $replacement, $string);
?>
Try this:
<?php
$url = "http://www.mywebsite.com/carmake/ABCDEFG/123456789";
$parts = parse_url($url);
$path = $parts['path'];
$pos = strpos($path, '/', 9);
$sub = substr($path, 9, $pos - 9);
$url = str_replace($sub, 'parts', $url);
Split to segments, change and collect back
$a = 'http://www.mywebsite.com/carmake/BNDFKNV/463634213';
$to = 'parts';
$s = explode('/', $a);
$s[count($s)-2] = $to;
echo implode('/', $s);

how to replace everything after specific word in php

$url =file("list.txt");
foreach ($url as $sites) {
$sites = trim($sites);
echo $sites . " </ br>";
}
and list.txt contain some urls
http://example.com/cms/wp-content/themes/
http://example.com/wp-content/plugins/
http://example.com/wp-content/themes/Avada-Child-Theme/
how could i remove the word "/wp-content/" and everything after it
to be
http://example.com/cms
http://example.com
http://example.com
Take a look at the the parameter $before_needle at http://docs.php.net/strstr
$o = strstr($url, '/wp-content/', true);
How about using preg_replace?
Something like that:
$sites = trim(preg_replace( '#/wp-content.*#', '', $sites));
This should work:
<?php
$url =file("list.txt");
foreach ($url as $sites) {
$sites = trim($sites);
$pos = strpos($sites, 'wp-content');
$newStr = substr($sites,0,$pos );
echo $newStr . " </ br>";
}
?>
$lines = file('list.txt');
$find = '/wp-content/';
foreach ($lines as $line) {
$line = trim($line);
$pos = strpos($line, $find);
if($pos !== false) {
echo substr($line, 0, $pos) . '<br>';
} else {
echo 'Not found ' . $find . '<br>';
}
}
First explode your content by new line then loop through each and use substr function to remove the matches. Following function my be useful to you:
<?php
// can remove variables from: full url, from urls related to site root, form just a query string like "a=1&b=2"
if(!function_exists("remove_var_from_url")){
function remove_var_from_url($variable_name, $url_string){
// this is anything before the "?" sign
$base_url = '';
// the variable separator, can be "?" if is a full URL or can be empty, if we just have "&sort=sales&oprder=asc"
$separator = "";
$start_pos = 0;
$return_string = "";
//
if(strpos($url_string,"?")!==false){
$start_pos = strpos($url_string, "?")+1;
$separator = "?";
$base_url = substr($url_string, 0, $start_pos-1);
}
// start building the string from the base url (which can be empty)
$return_string = $base_url;
$url_vars_string = substr($url_string, $start_pos);
$names_and_values = explode("&", $url_vars_string);
//
foreach($names_and_values as $value){
list($var_name, $var_value) = explode("=", $value);
if($var_name != $variable_name){
// add the "?" once if needed
if(!$separator_added){
$return_string.= $separator;
$separator_added = true;
} else {
$return_string.= "&";
}
$return_string.= $var_name."=".$var_value;
}
}
// remove "&" from margins
$return_string = trim($return_string, "&");
// remove the "?" if is at the end, it means it was just one variable that was removed
$return_string = rtrim($return_string, "?");
return $return_string;
}
}
?>
I would rather suggest you to apply strpos on each of the string first. Strpos will return you the position of first occurance of a string. Then use substr to fetch everything prior to that string.
` $lines = file('list.txt');
$find = '/wp-content/';
foreach ($lines as $line) {
$position = strpos($line, '/wp-content');
if($position)
$string = substr($line, 0, $position);
}`

How do I compare two domain names in URL strings?

Say if I have two strings
$first = 'http://www.example.com';
$second = 'www.example.com/';
How could I determine they match? I just care that the example part matches. I'm thinking some form of Regex pattern would match but I can't figure it out at all.
Don't use a regex if you're trying to evaluate structured data. Regexes are not a magic wand you wave at every problem that happens to involve strings. What if you have a URL like http://www.some-other-domain.com/blah/blah/?www.example.com?
If you're trying to match a domain name to a domain name, then break apart the URL to get the host and compare that. In PHP, use the parse_url function. That will give you www.example.com as the host name, and then you can compare that to make sure it is the hostname you expect.
Try this
function DomainUrl($x) {
$url = $x;
if ( substr($url, 0, 7) == 'http://') { $url = substr($url, 7); }
if ( substr($url, 0, 8) == 'https://') { $url = substr($url, 8); }
if ( substr($url, 0, 4) == 'www.') { $url = substr($url, 4); }
if ( substr($url, 0, 4) == 'www9.') { $url = substr($url, 4); }
if ( strpos($url, '/') !== false) {
$ex = explode('/', $url);
$url = $ex['0'];
}
return $url;
}
$first = DomainUrl('http://www.example.com');
$second = DomainUrl('www.example.com/');
if($first == $second){
echo 'Match';
}else{
echo 'Not Match';
}

PHP + Modify string

I have got a string and would like to remove everything after a certain "dot"+word combination. For instance:
This.Is.A.Test
=> would become
This.Is.A
Were you looking to remove everything after a specific dot+word, or just remove the last dot+word? If you're looking for a specific word, try this:
$str = "This.Is.A.Test";
$find = ".A";
$index = strpos($str, $find);
if ($index !== false)
$str = substr($str, 0, $index + strlen($find));
echo $str; // "This.Is.A"
In response to #SuperSkunk:
If you wanted to match the whole word, you could do this:
$find = ".A.";
$str = "This.Is.A.Test";
$index = strpos($str, $find);
if ($index !== false)
$str = substr($str, 0, $index + strlen($find) - 1);
echo $str; // "This.Is.A"
$str = "This.Is.AB.Test";
$index = strpos($str, $find);
if ($index !== false)
$str = substr($str, 0, $index + strlen($find) - 1);
echo $str; // "This.Is.AB.Test" (did not match)
$str = "This.Is.A.Test"; $str = substr($str, 0, strrpos($str, "."));
$result = explode('.', $str, 4);
array_pop($result);
implode('.', $result);
I'll do something very simple like :
<?php
$string = 'This.Is.A.Test';
$parts = explode('.', $string);
array_pop($parts); // remove last part
$string = implode('.', $parts);
echo $string;
?>
$pos = strpos($haystack, ".A" );
$result = substr($haystack,0,$pos);
...something like this.

Categories