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
Related
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 a url that will always look like some variation of this
https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg
I need to remove with PHP the resolution specifier "-150x150" so that it reads
https://sitename/wp-content/uploads/2017/09/59a778097ae6e.jpeg
If it's always -150x150 you can just use str_replace():
$url = "https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg";
$stripped = str_replace('-150x150', '', $url);
var_dump($stripped);
// string(62) "https://sitename/wp-content/uploads/2017/09/59a778097ae6e.jpeg"
If you need a way to strip out any resolution, you can use a regular expression for that:
$url = "https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg";
$stripped = preg_replace('/-[0-9]+x[0-9]+/', '', $url);
var_dump($stripped);
// string(62) "https://sitename/wp-content/uploads/2017/09/59a778097ae6e.jpeg"
hello you can use strpos() and substr() functions
<?php
$str1 = "https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg";
$str2 = "-150x150";
$pos = strpos($str1, $str2);
$part1 = substr($str1, $pos);
$part2 = substr($pos+1, strlen($str1));
$final_str = $part1.$part2;
echo $final_str;
?>
or you can also just use str_replace() and replace the part of the url by nothing :
<?php
$url = "https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg";
$str = "-150x150";
// will replace $str by '' in $url
$url = str_replace($str, '', $url);
echo $url;
?>
If it's not always 150x150, here's a nifty solution.
$url = 'https://sitename/wp-content/uploads/2017/09/59a778097ae6e-150x150.jpeg';
First get the extension
$ext = explode('.', $url);
$ext = $ext[count($ext)-1];
Then split by '-'
$array = explode('-', $url);
Pop the last array element which will be the resolution (150x150 here)
array_pop($array);
Then implode by '-' again and concatenate the extension to the new url
$new_url = implode('-', $array). '.' .$ext;
<?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);
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];
The xml is like this: (wordpress url's) I want to strip them and get only the posts words.
http://www.site1.com/dir/this-is-page/
http://www.site2.com/this-is-page
How do i strip the url's and get only "this is page" (without the rest of the urls, and the "-") if i have two diffrent types of urls; one with dir and one without dir? Sample code bellow:
$feeds = array('http://www.site1.com/dir/feed.xml', 'http://www.site2.com/feed.xml');
foreach($feeds as $feed)
{
$xml = simplexml_load_file($feed);
foreach( $xml->url as $url )
{
$loc = $url->loc;
echo $loc;
$locstrip = explode("/",$loc);
$locstripped = $locstrip[4];
echo '<br />';
echo $locstripped;
echo '<br />';
mysql_query("TRUNCATE TABLE interlinks");
mysql_query("INSERT INTO interlinks (title, url) VALUES ('$locstripped', '$loc')");
}
}
?>
TY
Ty guys, did it like this:
$urlstrip = basename($loc);
$linestrip = str_replace(array('-','_'), ' ', $urlstrip);
You want only the last segment of the URL?
Try something like this.
$url = trim('http://www.site1.com/dir/this-is-page/', '/');
$url = explode('/', $url);
$url = array_pop($url);
$url = str_replace(array('-','_'), ' ', $url);
It's not very elegant... but it works.
replace
$locstripped = $locstrip[4];
with
$locstripped = $locstrip[count($loc) - 1];
if(!$locstripped)
$locstripped = $locstrip[count($loc) - 2];
$locstripped = str_replace('-', ' ', $locstripped);