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);
Related
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:
how to get URL host using php
(2 answers)
Closed 8 years ago.
I know that there is parse_url and then you get get the ['host'], but that returns the full www.example.com. What I want is the following:
https://stackoverflow.com/questions/ask turns to stackoverflow
https://console.aws.amazon.com/s3/home?region=us-west-2 turns to amazon
https://www.google.com/ turns to google
Any suggestions on how to do that?
Still use parse_url(), but after that, use explode() and get the 2nd to the last index
// Explode by .
$arr_host = explode('.', $host);
// Count how many in array
$count = count($arr_host);
// Get second to the last index
$domain = $arr_host[$count-2];
echo $domain;
Try
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
echo strstr($regs['domain'], '.', true);
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Finetune Regex to skip tags
Currently my function looks like this. It converts plain text URLs into HTML links.
function UrlsToLinks($text){
return preg_replace('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.-]*(\?\S+)?)?)?)#', '$1', $text);
}
But there are some problems. What I'm trying to do is skip existing links, the src attribute in <img> tags, etc.. Can't figure out what I need to modify in this function.
This would work, assuming that the URLs we want to replace are not already inside a tag.
function UrlsToLinks($text){
$matches = array();
$strippedText = strip_tags($text);
preg_match_all('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.-]*(\?\S+)?)?)?)#', $strippedText, $matches);
foreach ($matches[0] as $match) {
if (filter_var($match, FILTER_VALIDATE_URL)) {
$text = str_replace($match, ''.$match.'', $text);
}
}
return $text;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace a list of emoticons with their images
i'm developing a website where i want to give users possibility to put smiles on posts.
My (just functional) idea is to use array in this way:
$emoticons = array(
array("17.gif",":)"),
array("6.jpg",":P"),
.....
array("9.jpg",":'("),
array("5.gif","X)")
);
with image on [0] and emoticon on [1].
And on each $post:
foreach($emoticons as $emoticon){
$quoted_emoticon = preg_quote($emoticon[1],"#");
$match = '#(?!<\w)(' . $quoted_emoticon .')(?!\w)#';
$post = preg_replace($match,'<img src="images/emoticons/'.$emoticon[0].'">',$post);
}
This is working good, but my problem is '#(?!<\w)(' and ')(?!\w)#' because I want emoticons to apply only when preceding characters are "begin" (^) or "blank" and succeeding characters are "end" ($) or "blank". What is the right regex to do this?
I think you want the positive look behind and positive look ahead.
Example:
(?<=\s|^)(\:\))(?=\s|$)
Your example updated:
foreach($emoticons as $emoticon){
$quoted_emoticon = preg_quote($emoticon[1],"#");
$match = '(?<=\s|^)(' . $quoted_emoticon .')(?=\s|$)';
$post = preg_replace($match,'<img src="images/emoticons/'.$emoticon[0].'">',$post);
}
I would go with:
$e = array( ':)' => '1.gif',
':(' => '2.gif',
);
foreach ($e as $sign => $file) {
$sign = preg_replace('/(.)/', "\\$1", $sign);
$pattern = "/(?<=\s|^)$sign(?=\s|$)/";
$post = preg_replace($pattern, " <img src=\"images/emoticons/$file\">", $post);
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php - efficent way to get and remove first line in file
I need to remove first line form txt file using php. Could you show me example code how to do it? I know that it's easy but I don't know php:) Thanks!
You could do this:
$file = file_get_contents(SOME_FILE);
$arr = explode('\n\r', $file);
if (isset($arr[0])) unset ($arr[0]);
$string = implode('\n\r', $arr);
file_put_contents(SOME_FILE, $string);
You could have used the search function at least. You would have found this:
$contents = file($file, FILE_IGNORE_NEW_LINES);
$first_line = array_shift($contents);
file_put_contents($file, implode("\r\n", $contents));