So, Jessica was kind enough to help me last time.
<?php
$string = 'hi+';
if(strpos($string, '+') !== false){
$string = str_replace('+', 'test', $string);
}
echo $string;
?>
Would basically work, however changing $string to a GET request like $string = $_GET['s']; and testing that would not take the + as user input (would just be blank). How would I fix this? Would it work with POST?
When in a GET parameter, the + gets urlencoded as %2B. When grabbing it from the query string, you need to urldecode it.:
$string = urldecode($_GET['s']);
Example
And if your url's aren't properly encoded, you can force encoding and you get desired result.
Something like that should work:
<?php
$string = urlencode($_GET['s']);
if(strpos($string, '+') !== false){
$string = str_replace('+', 'test', $string);
}
echo $string;
But i say: the correct way is decoding the encoded URL, not doing reverse (encoding to get the raw URL from already decoded one.)
Related
If I have a domain e.g. www.example.com/w/
I want to be able to get the whole string of text appended after the URL, I know how to get parameters in format ?key=value, that's not what I'm looking for.
but I would like to get everything after the /w/ prefix, the whole string altogether so if someone appended after the above
www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
I would be able to get https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
I was thinking of installing codeigniter on my server if that helps, but at the moment I'm just using core php
You just need to use str_replace()
$str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
$str2 = str_replace('www.example.com/w/', '', $str);
echo $str2;
Output
https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
Read more about str_replace()
Try this, with strpos and substr
$str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
echo $str.'<pre>';
$start = strpos($str, '/w/');
echo substr($str, $start + 3);die;
Output:
www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
strpos() will give you first occurrence of /w/ and from there you can do substr with +3 to remove /w/
OR Try this, with strstr and str_replace
$str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
echo $str.'<pre>';
$str1 = strstr($str, '/w/');
echo $str1.'<pre>';
$str2 = str_replace('/w/', '', $str1);
echo $str2.'<pre>';die;
Output:
www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
strstr() will give you substring with given /w/ and use str_replace() to remove /w/from new string
I have a string like this being entered into my database that I can't format before it gets stored :
image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg#32e2e9a111a4f9f4aa01dbad2ca2aa403c994d28
The only part of that string that I want to use is this :
hglz466d8mm1pazysaoh.jpg
I'm trying to use strpos to remove the excess data.
So far I've managed to remove everything after and including the hashtag
($data is the original string) :
$dataclean = substr($data, 0, strpos($data, "#"));
This works as expected with $dataclean returning :
image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg
But I don't know how to remove the rest of the excess data :
image/upload/v1440427262/
Also, can this all be done in one hit or does it have to be split into several operations?
Use basename:
$dataclean = basename(substr($data, 0, strpos($data, "#")));
If basename() doesn't work I would explode the string by the forward slash.
$data = 'image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg#32e2e9a111a4f9f4aa01dbad2ca2aa403c994d28';
$pieces = explode("/", $data);
$dataclean = substr($pieces[3], 0, strpos($data, "#"));
As mentioned in the question, first remove values after # using
$link = substr($data, 0, strpos($data, "#"));
Then use basename() function to access filename from the URL.
For example,
$link = "http://example.com/folderPath/filename.php";
echo basename($link); // It will return filename.php
Nothing wrong with substr + strpos, but it looks like your string is a URL, so you could use parse_url to isolate the path before using basename. Just another option FYI.
basename(parse_url($yourString, PHP_URL_PATH));
You can use a regex function:
preg_match("/[a-z0-9]+\.[a-z]{3}/i", $input_line, $output_array);
Try the code
I've a string in a variable titled $str as follows. This I got after converting it into JSON format. So the one more additional slash is added by JSON so please ignore it as it would not display while showing the string.
$str ="Let\\'s\nIt\\'s\nHe\\'s\nShe\\'s"; # \n is used for new line character, please ignore it
Now I want to check the presence of such backslash/es in a string and if they are present remove them and get the desired cleaned up string. In above case the output string should be(after converting it into JSON format) : "Let\'s\nIt\'s\nHe\'s\nShe\'s"
I tried below code but it didn't work out for me:
$str = br2nl(str_replace('\\','',$str));
function br2nl($buff = '') {
$buff = mb_convert_encoding($buff, 'HTML-ENTITIES', "UTF-8");
$buff = preg_replace('#<br[/\s]*>#si', "\n", $buff);
$buff = trim($buff);
return $buff;
}
Can some one please help me in this regard please?
As previously suggested, stripslashes() is the best way to do this:
<?php
$dirty ="Let\\'s\nIt\\'s\nHe\\'s\nShe\\'s";
$clean = stripslashes($dirty);
echo $clean."\n";
?>
Output:
Let's
It's
He's
She's
http://www.example.com/some_folder/some file [that] needs "to" be (encoded).zip
urlencode($myurl);
The problem is that urlencode will also encode the slashes which makes the URL unusable. How can i encode just the last filename ?
Try this:
$str = 'http://www.example.com/some_folder/some file [that] needs "to" be (encoded).zip';
$pos = strrpos($str, '/') + 1;
$result = substr($str, 0, $pos) . urlencode(substr($str, $pos));
You're looking for the last occurrence of the slash sign. The part before it is ok so just copy that. And urlencode the rest.
First of all, here's why you should be using rawurlencode instead of urlencode.
To answer your question, instead of searching for a needle in a haystack and risking not encoding other possible special characters in your URL, just encode the whole thing and then fix the slashes (and colon).
<?php
$myurl = 'http://www.example.com/some_folder/some file [that] needs "to" be (encoded).zip';
$myurl = rawurlencode($myurl);
$myurl = str_replace('%3A',':',str_replace('%2F','/',$myurl));
Results in this:
http://www.example.com/some_folder/some%20file%20%5Bthat%5D%20needs%20%22to%22%20be%20%28encoded%29.zip
Pull the filename off and escape it.
$temp = explode('/', $myurl);
$filename = array_pop($temp);
$newFileName = urlencode($filename);
$myNewUrl = implode('/', array_push($newFileName));
Similar to #Jeff Puckett's answer but as a function with arrays as replacements:
function urlencode_url($url) {
return str_replace(['%3A','%2F'], [':', '/'], rawurlencode($url));
}
How can I use str_replace method for replacing a specified portion(between two substrings).
For example,
string1="www.example.com?test=abc&var=55";
string2="www.example.com?test=xyz&var=55";
I want to replace the string between '?------&' in the url with ?res=pqrs&. Are there any other methods available?
You could use preg_replace to do that, but is that really what you are trying to do here?
$str = preg_replace('/\?.*?&/', '?', $input);
If the question is really "I want to remove the test parameter from the query string" then a more robust alternative would be to use some string manipulation, parse_url or parse_str and http_build_query instead:
list($path, $query) = explode('?', $input, 2);
parse_str($query, $parameters);
unset($parameters['test']);
$str = $path.'?'.http_build_query($parameters);
Since you're working with URL's, you can decompose the URL first, remove what you need and put it back together like so:
$string1="www.example.com?test=abc&var=55";
// fetch the part after ?
$qs = parse_url($string1, PHP_URL_QUERY);
// turn it into an associative array
parse_str($qs, $a);
unset($a['test']); // remove test=abc
$a['res'] = 'pqrs'; // add res=pqrs
// put it back together
echo substr($string1, 0, -strlen($qs)) . http_build_query($a);
There's probably a few gotchas here and there; you may want to cater for edge cases, etc. but this works on the given inputs.
Dirty version:
$start = strpos($string1, '?');
$end = strpos($string1, '&');
echo substr($string1, 0, $start+1) . '--replace--' . substr($string1, $end);
Better:
preg_replace('/\?[^&]+&/', '?--replace--&', $string1);
Depending on whether you want to keep the ? and &, the regex can be mofidied, but it would be quicker to repeat them in the replaced string.
Think of regex
<?php
$string = 'www.example.com?test=abc&var=55';
$pattern = '/(.*)\?.*&(.*)/i';
$replacement = '$1$2';
$replaced = preg_replace($pattern, $replacement, $string);
?>