I want to replace this URI
http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony&toto=titi
by this URI
http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&kljklj=sdfsd
==> I want to delete "&brand=Sony"
I tried this :
preg_replace('/(^.*)(&brand=.*)(&.*)/', '$1$3', 'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony&toto=titi');
but it doesn't work in a specific case : the case where the parameter "toto" in the URI doesn't exist
So if I do
preg_replace('/(^.*)(&brand=.*)(&.*)/', '$1$3', 'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony');
It doesn't work ==> "&brand=Sony" still appear
So how can I do ?
I wouldn't use regular expressions.
First, use parse_url to split the url into its bits and pieces.
Then, use parse_str on the query portion.
Do whatever you want to the query keys, then combine it all back.
To build the query string back: http_build_query
Then build the url using http_build_url
preg_replace("/\&brand=Sony/", "", $uri);
How about:
preg_replace('/[\?&]brand=\w*/', '', $url);
If you want the value of key brand
preg_replace('/&?brand=[^&]+/i','',$url);
(^.*)(&brand=.*)(&.*)?
you can just add ?:
(^.*)(&brand=.*)(&.*)?
echo preg_replace(
'/(^.*)(&brand=.*)(&.*)?/',
'$1$3',
'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony');
output:
http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0
Thank you everybody. So here is my final solution and it works fine :
<?php
$actual_link = 'index.php?'.$_SERVER['QUERY_STRING']; //complete link of my page
$parsedURL= parse_url($actual_link);
parse_str($parsedURL["query"],$tabParametersQuery);
$tabParametersQuery['brand']="";
$newURL = "index.php?".http_build_query($tabParametersQuery);
?>
Related
My url like this:
http://mywebsite.com/movies/937-lan-kwai-fong-2?file=Rae-Ingram&q=
http://mywebsite.com/movies/937-big-daddy?file=something&q=
I want to get "lan-kwai-fong-2" and "big-daddy", so I use this code but it doesn't work. Please help me fix it ! If you can shorten it, it is so great !
$url= $_SERVER['REQUEST_URI'];
preg_replace('/\?file.*/','',$url);
preg_match('/[a-z][\w\-]+$/',$url,$matches);
$matches= str_replace("-"," ",$matches[0]);
First there are issue with your code which im going to go over because they are general things:
preg_replace does not work by reference so you are never actually modifying the url. You need to assign the result of the replace to a variable:
// this would ovewrite the current value of url with the replaced value
$url = preg_replace('/\?file.*/','',$url);
It is possible that preg_match will not find anything so you need to test the result
// it should also be noted that sometimes you may need a more exact test here
// because it can return false (if theres an error) or 0 (if there is no match)
if (preg_match('/[a-z][\w\-]+$/',$url,$matches)) {
// do stuff
}
Now with that out of the way you are making this more difficult than it needs to be. There are specific function for working with urls parse_url and parse_str.
You can use these to easily work with the information:
$urlInfo = parse_url($_SERVER['REQUEST_URI']);
$movie = basename($urlInfo['path']); // yields 937-the-movie-title
Just replace
preg_replace('/\?file.*/','',$url);
with
$url= preg_replace('/\?file.*/','',$url);
Regex works, and parse_url is the right way to do it. But for something quick and dirty I would usually use explode. I think it's clearer.
#list($path, $query) = explode("?", $url, 2); // separate path from query
$match = array_pop(explode("/", $path)); // get last part of path
How about this:
$url = $_SERVER['REQUEST_URI'];
preg_match('/\/[^-]+-([^?]+)\?/', $url, $matches);
$str = isset($matches[1]) ? $matches[1] : false;`
match last '/'
match anything besides '-' until '-'
capture anything besides '?' until (not including) '?'
I have a string http://localhost:9000/category that I want to replace with category.html, i.e. strip everything before /category and add .html.
But can't find a way to do this with str_replace.
You want to use parse_url in this case:
$parts = parse_url($url);
$file = $parts['path'].'.html';
Or something along that line. Experiment a bit with it.
Ismael Miguel suggested this shorter version, and I like it:
$file = parse_url($url,PHP_URL_PATH).'.html';
Much better than a ^*!$(\*)+ regular expression.
.*\/(\S+)
Try this.Replace by $1.html.see demo .
http://regex101.com/r/nA6hN9/43
Use preg_replace instead of str_replace
Regex:
.*\/(.+)
Replacement string:
$1.html
DEMO
$input = "http://localhost:9000/category";
echo preg_replace("~.*/(.+)~", '$1.html', $input)
Output:
category.html
A solution without regex:
<?php
$url = 'http://localhost:9000/category';
echo #end(explode('/',$url)).'.html';
?>
This splits the string and gets the last part, and appends .html.
Note that this won't work if the input ends with / (e.g.: $url = 'http://localhost:9000/category/';)
Also note that this relies on non-standard behavior and can be easily changed, this was just made as a one-liner. You can make $parts=explode([...]); echo end($parts).'.html'; instead.
If the input ends with / occasionally, we can do like this, to avoid problems:
<?php
$url = 'http://localhost:9000/category/';
echo #end(explode('/',rtrim($url,'/'))).'.html';
?>
i have a php variable $link="http://localhost/mysite/?product=men-glasses1", and I would like to display only the test after the "=" , that is here men-glasses1.
Can someone help?.
Use parse_url to return params section then parse_str to convert it into an associative array
$link="http://localhost/mysite/?product=men-glasses1";
parse_str( parse_url( $link, PHP_URL_QUERY ), $output );
echo $output["product"];
// men-glasses1
see this demo
You can use strstr and substr:
echo substr(strstr($link, "="),1);
http://php.net/manual/de/function.strstr.php
http://php.net/manual/de/function.substr.php
You could do the following in this case:
$var = explode('=',$link);
echo $var[1]; //men-glasses1
or if it's just in stage of URL (not in a variable yet), then you can just simply access it this way:
$_GET['product'];
Or as your link may get more and more sophisticated you may have a look at regular expressions. I would recommend using this tool to generate your regexes.
Use strstr() function in php to do this.This function searches for the first occurrence of a string inside another string Your code should look like this
<?php
$link="http://localhost/mysite/?product=men-glasses1";
echo strstr($link, "=");
?>
Hope this helps you
I'm extracting twitter user's profile image through JSON. For this my code is:
$x->profile_image_url
that returns the url of the profile image. The format of the url may be "..xyz_normal.jpg" or "..xyz_normal.png" or "..xyz_normal.jpeg" or "..xyz_normal.gif" etc.
Now I want to delete the "_normal" part from every url that I receive. How can I achieve this in php? I'm tired of trying it. Please help.
Php str_replace.
str_replace('_normal', '', $var)
What this does is to replace '_normal' with '' (nothing) in the variable $var.
Or take a look at preg_replace if you need the power of regular expressions.
The str_ireplace() function does the same job but ignoring the case
like the following
<?php
echo str_ireplace("World","Peter","Hello world!");
?>
output : Hello Peter!
for more example you can see
The str_replace() function replaces some characters with some other characters in a string.
try something like this:
$x->str_replace("_normal","",$x)
$s = 'Posted On jan 3rd By Some Dude';
echo strstr($s, 'By', true);
This is to remove particular string from a string.
The result will be like this
'Posted On jan 3rd'
Multi replace
$a = array('one','two','three');
$var = "one_1 two_2 three_3";
str_replace($a, '',$var);
string erase(subscript, count)
{
string place="New York";
place erase(0,2)
}
I would like to replace ":" with "\xEF\xBC\x9A" however, I do not want to do this for : that follows "http", "https", and string with the format of #[{numbers}:{numbers}:{some text}].
What I have doesn't really work as (?<!#\[\d) does not check what's after it. My current implementation only work for something like #[1:2:text]. Thanks!
$string=preg_replace('#(?<!https)(?<!http)(?<!#\[\d)(?<!#\[\d:\d):#', "\xEF\xBC\x9A", $string);
Try this:
preg_replace('/(#\[\d+:\d+:[^]]*]|https?:)|:/e', '"$1"?"$1":"\xEF\xBC\x9A"', $string);
Try this regex:
(?<!#\[(?::?[\d]{0,5}){1,2})(?<!https?):
It should match the first and second instances of ':' here.
: test: http: #[1:2:text]
Usage Sample:
$string = preg_replace('/(?<!#\[(?::?[\d]{0,5}){1,2})(?<!https?):/', '\xEF\xBC\x9A', $string);