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
Related
I am trying to change all the links of a html with php preg_replace. All the uris have the following form
test.com/item/16
I want to change it to:
test.com/z/item/16
I tried the following, but it returns no changes:
$links = 'http://test.com/item/16';
preg_replace("/item","z/item",$links);
echo $links;
// output >>> http://test.com/z/item/16
You have to use delimiters as #nickb has pointed out, i.e. /your_regular_expression/. The / is the standard delimiter for regular expressions, and so, it being a special character, you'd have to escape the / you want to match by using a backslash, \/:
preg_replace("/\/item/","z/item",$links);
But luckily, you can choose your own delimiters, like #, so then no need to escape the /:
preg_replace("#/item#","z/item",$links);
Do this:
<?php
$links = 'http://test.com/item/16';
$a = preg_replace("/item/","z/item",$links);
echo $a;
preg_replace does not change the input string but instead returns a modified string....which is stored in $a variable..
You need delimiter and return of preg_replace set to variable
$links = 'http://test.com/item/16';
$links = preg_replace('/\/item/','/z/item',$links);
echo $links;
But, why don't you use just str_replace in this case?
The problem with the provided answers is that if you have more than one instance of /item in the URL, all of them will get replaced, for example a URL like:
http://items.domain.com/item/16
would get messed up, try modifying just the path:
$path = parse_url( $url, PHP_URL_PATH );
$url = str_replace( $path, '/z'.$path, $url );
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 this code, is working in all the tester I'm using for regex, but later, in my real php code it doesn't work. What I want is to replace the number in the link for something else
$value='/something.html?helperid=252';
//patern
$patternHelperId='/(?<=helperid=)\d{1,}/';
//replace
preg_replace($patternHelperId, "mynewreplacement", $value);
//debug
echo "\n$value\n";// /something.html?helperid=252????? aggain???
What's wrong??
You should assign the result of preg_replace back to $value, like this:
$value = preg_replace($patternHelperId, "mynewreplacement", $value);
And, as a sidenote, \d{1,} can be replaced with \d+.
preg_replace returns the result. It does not modify the variable in-place
You have forgotten to take the result of the preg_replace function:
$newValue = preg_replace($patternHelperId, "mynewreplacement", $value);
echo "\n$newvalue\n";
A better pattern:
$patternHelperId='/helperid=\K\d++/';
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);
?>
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)
}