Image upload preg_match check file [duplicate] - php

This question already has answers here:
Alternative for deprecated PHP function: eregi_replace [duplicate]
(2 answers)
Closed 8 years ago.
I'm trying to fix a picture upload.
But I have some troubles with the check if the file is a picture.
Before I used eregi_replace but that function is deprecated and now I'm wondering is there possible to use preg_match and check in an array for a match or how should I do this?
$name = "Picture.jpg";
$picture = array("/^JPG/", "/^PNG/", "/^GIF/", "/^gif/", "/^png/", "/^jpg/", "/^JPEG/", "/^jpeg/");
$something = preg_match($picture, $name, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($something);
Like that or something? I dont have a clue, hope you can help me!

Like below:
$name = "Picture.jpg";
if (preg_match('/\.(jpe?g|png|gif)$/i', $name, $matches)) {
//...
}
But note this only check the filename, if you want to check it's a real image, you should validate the actual mime-type.

Related

What to replace eregi() with? [duplicate]

This question already has answers here:
ereg/eregi replacement for PHP 5.3 [duplicate]
(4 answers)
Closed 5 years ago.
What is eregi() replaced with in this instance?
// get value of text inbetween tags
function getContentByTag($tag1, $tag2, $string)
{
if (eregi("$tag1(.*)$tag2", $string, $out)) {
$outdata = $out[1];
}
return $outdata;
}
This post is not a duplicate post as the 3rd example within the referenced post the answer is for that specific usage. I'm guessing my usage is different as the referenced post answers are not working for me.
Since PHP 7 you must replace it with
preg_match("/$tag1(.*)$tag2/i", $string, $out)

PHP Replace text in string [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php

how to check if string has server ih php [duplicate]

This question already has answers here:
Validating a URL in PHP [duplicate]
(3 answers)
Closed 6 years ago.
How to check if string has a server depending if url is absolute or relative?
function hasServer($url){
...
}
If this, returns true
hasServer('http://www.google.com/');
else return false
hasServer('/about-us/team/');
I think this is what you're looking for:
$url = 'http://www.google.com/';
$hasServer = filter_var($url, FILTER_VALIDATE_URL);
There are actually a few options from my experience. One would be to use the headers() function and then to analyse what information you obtained inside the resulting array.
$arrayResult = headers('http://www.google.com/');
foreach ($arrayResult as $value)
{
echo "-- ".$value."<br>";
}
The output should give you all the information you need regarding if the url actually exists.
A simpler solution in my opinion is to just check if the fopen() function actually works on the url!
if (fopen('http://www.google.com/', "r")
{
echo "the URL exists!<br>";
}
You can choose which one servers your needs better.

Remove url from string using PHP [duplicate]

This question already has answers here:
Get path from URL
(2 answers)
Closed 8 years ago.
Can anyone please tell me how to remove url from a string using PHP
I have this string
src="http://cdn1.vox-cdn.com/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png"
Desired Output:
src="/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png"
You can use the parse_url() buil-in php function for this.
For example:
<?php
$url = 'http://cdn1.vox-cdn.com/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png';
$parsed_url = parse_url($url);
var_dump($parsed_url);
?>
Output:
So, after the url is parsed, the bit you want is found on $parsed_url['path']. Hope this helps.

How to extract last part in PHP? [duplicate]

This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php

Categories