<?php
$url = "http://localhost/news&lang=en&lang=sk&lang=sk&lang=sk&lang=en";
$langs = array ('sk', 'en');
foreach ($langs as $lang) {
$search = '&lang='.$lang;
$new = str_replace($search, "", $url);
}
echo $new; // output: http://localhost/news
?>
Q: How to delete all parameters (&lang=en, &lang=sk) from string ?
Thank you in advance
What you are doing is creating a new variable $new each time so that won't do anything good with the $url.
Try to assign the str_replace back to its original variable like:
$url = "http://localhost/news&lang=en&lang=sk&lang=sk&lang=sk&lang=en";
$langs = array ('sk', 'en');
foreach ($langs as $lang) {
$search = '&lang='.$lang;
$url = str_replace($search, "", $url);
}
echo $url; // output: http://localhost/news
You want to use parse_url() http://www.php.net/manual/en/function.parse-url.php and then http_build_query() http://php.net/manual/en/function.http-build-query.php
An Alternative:
First:
$url = "http://localhost/news&lang=en&lang=sk&lang=sk&lang=sk&lang=en";
echo preg_replace("#&lang=(en|sk)#", "", $url);
Second:
$url = "http://localhost/news&lang=en&lang=sk&lang=sk&lang=sk&lang=en";
echo str_replace(array("&lang=en", "&lang=sk"), "", $url);
Update: for long array of $lang:
$url = "http://localhost/news&lang=en&lang=sk&lang=sk&lang=sk&lang=en";
echo preg_replace("#&lang=(".implode("|", $lang).")#", "", $url);
Related
I have made a wordpress plugin for image crwaler but i have proplem in this code
so when i print $image i get this output https:// localhost/wordpress
there is a space after https://
i tryed str_replace but did not gone
i want the result https://localhost/wordpress
<?php
function image_url_filter($url) {
$url = str_replace('?ssl=1', '', $url);
$url = str_replace('https://', '', $url);
$url = str_replace('http://', '', $url);
$url = str_replace('//', '', $url);
$url = str_replace('http:', '', $url)
return "https://{$url}";
}
function get_chapter_images() {
include('simple_html_dom.php');
$url = 'http://localhost/wordpress/manga/manga-name-ain/chapter-4/';
$html = file_get_html($url);
$images_url = array();
foreach ($html->find('.page-break img') as $e) {
$image_links = $e->src;
array_push($images_url, image_url_filter($image_links));
}
//print_r($images_url);
return $images_url;
}
$images_links = get_chapter_images();
foreach ($images_links as $image) {
print_r($image);
}
%09 and + means you have both tab and space in your string, so you need to use urldecode(), together with str_replace() to fix that:
<?php
$url = 'https%3A%2F%2F%09%09%09+%09%09%09localhost%2Fwordpress%2Fwp-content%2Fuploads%2FWP-manga%2Fdata%2Fmanga_5e62092804a6d%2Ff6954e41130c0015b5b89a3021d55595%2F12.jpg';
$url_decode = urldecode($url);
$url_decode = str_replace(" ", "", $url_decode);
$url_decode = str_replace("\t", "", $url_decode);
echo $url_decode;
output:
https://localhost/wordpress/wp-content/uploads/WP-manga/data/manga_5e62092804a6d/f6954e41130c0015b5b89a3021d55595/12.jpg
Note: Don't forget to use double quotas when replacing tab or newline
I have variables that gets url. Then from this url I remove another url. First url removes another url but second not because it contains Russians words. How I can remove from url Russians letters:
$url = $_SERVER['REQUEST_URI'];
$url2 = $_SERVER['REQUEST_URI'];
if (isset($_GET['page'])) {
page = $_GET['page'];
}
if (isset($_GET['category'])) {
$category = $_GET['category'];
}
$url = str_replace('&page='.$page, "", $url); // works
$url2 = str_replace('&category='.$category, "", $url2); // does not working
echo $url2; // i check and $url2 does not remove category, because it contains Russians words
With the help of http_build_query (or its polyfill) in your environment, you can write a simple function to rewrite query parameters on the fly instead of using str_replace.
For example, to rewrite the "category" parameter, you may
<?php
function uri_rewrite_query($uri, $callback) {
$parsed = parse_url($uri);
parse_str($parsed['query'] ?? '', $query);
$parsed['query'] = http_build_query($callback($query));
return http_build_url($uri, $parsed);
}
function query_remove_category($query) {
unset($query['category']);
return $query;
}
function query_replace_category($category) {
return function ($query) use ($category) {
$query['category'] = $category;
return $query;
};
}
Then you can do these:
<?php
$uri = '/beverages.php?lang=ru&category=some_category';
echo uri_rewrite_query($uri, 'remove_category');
// Result: /beverages.php?lang=ru
echo uri_rewrite_query($uri, query_replace_category('Безалкогольные напитки'));
// Result: /beverages.php?lang=ru&category=%D0%91%D0%B5%D0%B7%D0%B0%D0%BB%D0%BA%D0%BE%D0%B3%D0%BE%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5+%D0%BD%D0%B0%D0%BF%D0%B8%D1%82%D0%BA%D0%B8 (equivalant to "/beverages.php?lang=ru&category=Безалкогольные напитки")
Or if you're only interested in the query string:
function uri_get_query() {
$parsed = parse_url($uri);
parse_str($parsed['query'] ?? '', $query);
return $query;
}
echo '/food.php?' . http_build_query(query_remove_category($_SERVER['QUERY_STRING'] ?? ''));
echo '/food.php?' . http_build_query(query_replace_category('Безалкогольные напитки')($_SERVER['QUERY_STRING'] ?? ''));
Try searching for the occurrence of the string using urlencode on str_replace(), like so:
$url2 = str_replace('&category='. urlencode($category), "", $url2);
I have below code:-
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about
I want URL element that is - http://www.example.com/about
You can use PHP's inbuilt parse_url and parse_str functions.
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
parse_str(parse_url($link,PHP_URL_QUERY),$url);
echo $url['url'];
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
$url = explode("&url=", $link)[1];
echo $url; //http://www.example.com/about
Try this:
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
$uri=explode("url=",$link);
$uri=$uri[count($uri)-1];
echo $uri;
function getSomething($url) {
if (!(strpos($url, 'url=') !== false)) return false;
$parse = explode('url=', $url);
$code = $parse[1];
return $code;
}
echo getSomething("http://www.domain.com/go.php?id=545&url=http://www.example.com/about");
this will work even if url is placed somewhere else as parameter
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
$url = "";
$params = explode("&", $link);
for($params as $param){
if(substr($param, 0, 4) === 'url='){
$url = explode("=", $param)[1];
break;
}
}
echo $url; //http://www.example.com/about
I have this url /index.php?color=blue&size=xl
to get rid of the get parameter, I use this code:
$done = preg_replace('/(.*)(\?|&)color=[^&]*(?(1)&|)?/i', "$1", $url);
echo $done;
"output: index.phpsize=xl"
Now I need to clean the "size" part too. Have tried with two lines of preg_replace, but it doesn´t work.
$done = preg_replace('/(.*)(\?|&)color=[^&]*(?(1)&|)?/i', "$1", $url);
echo $done;
$done2 = preg_replace('/(.*)(\?|&)size=[^&]*(?(1)&|)?/i', "$1", $done);
Edit: I really need a solution where I can clean the exact parameter "color" or "size".
Sometimes I will only delete one of them.
Edit2:
Have this solution:
// Url is: index.php?color=black&size=xl&price=20
function removeqsvar($url, $varname) {
return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url);
}
$url = removeqsvar($url, color);
echo removeqsvar($url, price);
// will output: index.php?size=xl
Thank you all.
This will allow you to exactly specify which parameters to remove using the $remove array. It works by parsing the URL with parse_url(), then grabbing the query string and parsing it with parse_str().
From there, it's straightforward - Iterate over the parameters in the URL, if one of them is in the $remove array, then delete it from the $params array. By the end, if we have parameters to add to the URL, we add them back with http_build_query().
$url = '/index.php?color=blue&size=xl'; // Your input URL
$remove = array( 'color', 'size'); // Change this to remove what you want
$parts = parse_url( $url);
parse_str( $parts['query'], $params);
foreach( $params as $k => $v) {
if( in_array( $k, $remove)) {
unset( $params[$k]);
}
}
$url = $parts['path'] . ((count( $params) > 0) ? '?' . http_build_query( $params) : '');
echo $url;
list($done) = explode("?", $url);
This snytax also works in PHP 5.3 and lower
try this:
$result = explode('?', $url)[0];
for a php version lower than php 5.4:
$tmp = explode('?', $url);
$result = $tmp[0];
$url= "justfwalk.it?uid=12";
How can I get the variable "uid" from this string?
Is there is an easier method than looking for it's position and trimming the string?
How about parse_url and parse_str?
<?php
$params = array();
$url= "justfwalk.it?uid=12";
$url_parts = parse_url( $url);
parse_str( $url_parts['query'], $params);
echo $params['uid']; // Outputs 12
See it in action
Sure, use parseurl
<?php
$url = 'http://localhost/'.$url;
print_r(parse_url($url));
?>
list($html, $get) = explode('?', $url);
$get = explode('=', $get);
print_r($get);
$parts = explode("=", $url);
$uid = $parts[1];