This question already has answers here:
post processing on google urls
(2 answers)
Closed 8 years ago.
How can this string '/url?q=' that sits before each Google url be removed? I have tried regular expression, but it is not working.
<?php
$url = "/url?q=http://www.testinstrumentsafrica.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CBMQFjAAOGQ&usg=AFQjCNGIavZUP46nbvLPJUrDXTgC3QF6aQ";
echo preg_replace("%/url?q=%", " ", $url);
?>
You don't really need regular expresions for fixed strings:
$url = "/url?q=http://www.testinstrumentsafrica.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CBMQFjAAOGQ&usg=AFQjCNGIavZUP46nbvLPJUrDXTgC3QF6aQ";
$url = mb_substr($url, 7);
var_dump($url);
... or plain substr() if you aren't using UTF-8.
Try this:
echo preg_replace("%/url\\?q=%", " ", $url);
You need to escape ? or otherwise it represents an optional l
echo preg_replace("~\/url\?q=~", " ", $url);
The above code would replace the string /url?q= with a space.
DEMO
you could try this:
$parts = explode('/url?q=', $url);
$justUrl = $parts[0];
Related
This question already has answers here:
How to remove the querystring and get only the URL?
(16 answers)
Closed 2 years ago.
I would like to remove anything that follows after a specific set of characters (i.e. filetypes / extensions). I have tried numerous scripts I found online, but none really manage to do what I need, they either remove the file extension as well, or keep parts of the arguments that follow.
$urls = array(
'http://www.example.com/images/image1.jpg',
'http://www.example.com/images/image2.png?arg=value',
'http://www.example.com/images/image3.jpg?foo=bar',
'http://www.example.com/images/image4.gif?v=1',
'http://www.example.com/images/image5.bmp?x=y',
'http://www.example.com/images/image6.tiff?werdfs=234234'
);
Desired outcome:
http://www.example.com/images/image1.jpg
http://www.example.com/images/image2.png
http://www.example.com/images/image3.jpg
http://www.example.com/images/image4.gif
http://www.example.com/images/image5.bmp
http://www.example.com/images/image6.tiff
Maybe this one help you.
$re = '/^.*(?:\.)[a-zA-Z]+/m';
$urls = array(
'http://www.example.com/images/image1.jpg',
'http://www.example.com/images/image2.png?arg=value',
'http://www.example.com/images/image3.jpg?foo=bar',
'http://www.example.com/images/image4.gif?v=1',
'http://www.example.com/images/image5.bmp?x=y',
'http://www.example.com/images/image6.tiff?werdfs=234234',
'asdasd'
);
foreach ($urls as $url) {
preg_match($re, $url, $matches);
if ($matches) {
echo $matches[0];
echo "\n";
}
}
Output
http://www.example.com/images/image1.jpg
http://www.example.com/images/image2.png
http://www.example.com/images/image3.jpg
http://www.example.com/images/image4.gif
http://www.example.com/images/image5.bmp
http://www.example.com/images/image6.tiff
How about PHP's parse_url() and basename?
$inName = $urls[0]; // for example
$newName = parse_url($inName,PHP_URL_SCHEME)
. parse_url($inName,PHP_URL_HOST)
. parse_url($inName,PHP_URL_PATH)
. basename($inName);
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 3 years ago.
I need someone who can make the following output which is a single string
[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1
to
{"mobile":"XXX-XXX-XXXX",
"permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}",
"tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}
Help me removing first '[' and last ']1' in the above string. Thanks in advance
This should work for you.
$final_str = rtrim(ltrim($your_str, '['), ']1');
Try below code,
<?php
$str = '[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1';
$temp_str = preg_replace('/\[/',"",$str);
$new_str = str_replace("]1","",$temp_str);
echo $new_str;
?>
Output,
{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}
This will remove first and last character from your string
$result = substr($string, 1, -2);
Here is the link if you want to explore more:
https://www.w3schools.com/php/func_string_substr.asp
Try ltrim() and rtrim()
$str = '[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1';
$getTrim = ltrim($str, '[');
$getTrim = rtrim($getTrim, ']1');
echo $getTrim;
OR
$getTrim = rtrim(ltrim($str, '['), ']1');
you can use str_replace or preg_replace replace your keyword with empty string.
This question already has answers here:
Parsing domain from a URL
(19 answers)
Closed 8 years ago.
http://www.example.com/hu/link
Using PHP how can I only keep the hu? Please note that the link is only one variable, it may be anything
You can use explode
$exploded = explode('/', 'http://www.example.com/hu/link');
$value = $exploded[3];
One solution is this:
$split = explode("/", $_SERVER["REQUEST_URI"]);
echo $split[1];
$url = "http://www.example.com/hu/link";
$split_url = explode('/', $url);
echo $split_url[3];
Output:
hu
You can use a regular expression preg_match like this:
$url = 'http://www.example.com/hu/link';
preg_match('/.*www.*\/(.*)\//i',$url, $matches);
print_r($matches[1]);
This question already has answers here:
PHP Regex to Remove http:// from string
(8 answers)
Closed 9 years ago.
I have a PHP script that removes the "http://" from user input url strings.
My Script:
$url= "http://techcrunch.com/startups/";
$url = str_replace('http://', '', $url);
Result:
$url= techcrunch.com/startups/
This works great, except that sometimes urls have "https://" instead. Is there a way I can just remove everything before the domain name, no matter what it is?
Try this out:
$url = 'http://techcrunch.com/startups/';
$url = str_replace(array('http://', 'https://'), '', $url);
EDIT:
Or, a simple way to always remove the protocol:
$url = 'https://www.google.com/';
$url = preg_replace('#^.+?\:\/\/#', '', $url);
Something like this ought to do:
$url = preg_replace("|^.+?://|", "", $url);
Removes everything up to and including the ://
Use look behinds in preg_replace to remove anything before //.
preg_replace('(^[a-z]+:\/\/)', '', $url);
This will only replace if found in the beginning of the string, and will ignore if found later
preg_replace('/^[^:\/?]+:\/\//','',$url);
some results:
input: http://php.net/preg_replace
output: php.net/preg_replace
input: https://www.php.net/preg_replace
output: www.php.net/preg_replace
input: ftp://www.php.net/preg_replace
output: www.php.net/preg_replace
input: https://php.net/preg_replace?url=http://whatever.com
output: php.net/preg_replace?url=http://whatever.com
input: php.net/preg_replace?url=http://whatever.com
output: php.net/preg_replace?url=http://whatever.com
input: php.net?site=http://whatever.com
output: php.net?site=http://whatever.com
$new_website = substr($str, ($pos = strrpos($str, '//')) !== false ? $pos + 2 : 0);
This would remove everything before the '//'.
EDIT
This one is tested. Using strrpos() instead or strpos().
Using str_replace I want to change $url from this:
$url = http://example.com/images/lala1.jpg
to this
$url = http://example.com/images/lala1-0001.jpg
My problem is that I don't know how to insert the "-".
$url is changing so I really only know that it has ".jpg" at its' end.
My code so far:
for($i=1;$i<=9;$i++) {
$array[] = str_replace('.jpg',sprintf("%04d",$i).'.jpg',$url); }
Any idea how I can make this work?
You might want to make use of a regular expression:
$str = preg_replace('/\.jpg$/', sprintf("-%04d.jpg",$i), $url);
This insures that it ONLY gets replaced at the end of the string.
Sprintf allows for regular characters in the arguments:
str_replace('.jpg', sprintf("-%04d.jpg", $i), $url);