Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This one may be a bit of a doozy.
I have 2 sets of files in separate folders:
SET 1:
celeb1.png
celeb2.png
celeb3.png
celeb4.png
celeb5.png
etc
The user selects an image (radio) and I then set this as a get variable on the url:
Now, I want to match another file.
In another folder, I have a set like this:
SET 2:
celeb1-24_59_250_300.png
celeb2-35_67_200_250.png
celeb3-54_87_300_400.png
celeb4-88_98_250_350.png
celeb5-87_43_300_400.png
etc
I want to use the GET variable (i.e.) celeb1.png, and then select the full filename of the corresponding file, based on the first 5 characters.
In other words if $_GET['celebimg'] is 'celeb1.png', I want to set a second variable (let's say $overlay) to string 'celeb1-24_59_250_300.png'
Any idea on how to achieve that?
I could obviously do a switch, but that means I would have to update the code every time a new celeb image is uploaded.
Is there any dynamic way to achieve this?
Thanks
JG
You can try something like this. Since we haven't seen what you have tried so far. This is just an illustration. However the code has been tested.
<?php
$file='celeb3.png'; // Here is where you will get the param $_GET['set1']
$set2=array(
'celeb1-24_59_250_300.png',
'celeb2-35_67_200_250.png',
'celeb3-54_87_300_400.png',
'celeb4-88_98_250_350.png',
'celeb5-87_43_300_400.png'
);
$set1 = explode('.',$file);
//echo $set1[0];//celeb3
foreach($set2 as $val)
{
if($set1[0]==substr($val,0,strlen($set1[0])))
{
echo $val;//celeb3-54_87_300_400.png
break;
}
}
OUTPUT : celeb3-54_87_300_400.png
Use glob to search a folder
//Get the GET variable
$cimg = $_GET['celebimg'];
//split it so we can just get the name without extention
$nameparts = explode(".",$cimg);
//glob will try to find matches to the string passed
$matches = glob("/dir/path/{$nameparts[0]}-*.png");
$matches will be an array of matched files
you could use other file functions like scandir or readdir etc but those do not filter out the files so you would have to do it.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a folder with many file , and i need to perform a deletion with certain file
and those file have a pattern like
messages.bm.inc.php
messages.cn.inc.php
messages.en.inc.php
Those file are dynamically created , but the pattern is there
Before this i normally delete my file with below code , and repeat it
$filename="messages.en.inc.php";
if (file_exists($filename)) {
unlink($filename);
}
Now i having a more dynamic situation , i need search through those file with the patern and delete it , please suggest a way to do , thanks
$files = glob("path_to_your_files/messages.*.inc.php ");
array_map('unlink', $files);
By glob you will get all your files from folder by specified pattern, array_map will implement unlink function for array of matched files.
foreach (glob("messages.*.inc.php") as $filename) {
unlink($filename);
}
Use the PHP glob() function to get the list of the files by pattern and delete using the loop.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my Laravel-based site, I need to find # and # within text content and replace it with a URL, such as a URL pointing at a user's Twitter page. How can I:
reliably find these strings within text portions of the HTML
replace found instances with a URL
The code for it is vast. You will have to use ajax here, in the textarea/textbox you will have to use "onkeyup" event, every key pressed have to be compared with "#" or "#" then the next character right after "#" has to be searched in the database.
So lets saw the user has typed "#A" till now and the user aims to type "#Ankur" Then as soon as "A" is typed the ajax script will start searching for users in the database and it is retrieved with the name, url and you just have to echo it on the screen.
THis is what you are looking for.. https://stackoverflow.com/a/4277114/829533
$strTweet = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/', '\1#\2', $strTweet);
And https://stackoverflow.com/a/4766219/829533
$input = preg_replace('/(?<=^|\s)#([a-z0-9_]+)/i', '#$1', $input);
Using regex it's rather simple. I would make one function that takes a prefix and replacement, like so.
function tweetReplaceObjects($input, $prefix, $replacement) {
return preg_replace("/$prefix([A-Za-z0-9_-])/", $replacment, $input);
}
An example usage would be something like this.
$text = 'hey look, it\'s #stackoverflow over there';
// expected output:
// hey look, it's stackoverflow over there
echo tweetReplaceObjects($text, '#', '$1');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm getting info out of a RSS feed.
How can I change a file name with php?
Say I have this filename:
http://anyurl.com/any_file_name_200.jpg
I need to change the last part of it "200.jp" into another number, say "800.jpg". I need to do this dynamically, because "any_file_name" will always be different, but all the file names have the same structure in the end "123.jpg"
Any ideas?
You can use preg_replace:
$filename = 'any_file_name_200.jpg';
$newFilename = preg_replace('/\d{3}\.jpg/','800.jpg',$filename);
It depends what do you mean by "always the same structure".
If you mean XXX.jpg (Three digits + ".jpg"), then you simply have to remove the 7 last char and replace them by whatever you want. Many ways to do that, check PhP functions related to string in the PhP manual.
If you mean it will always end by _XXXXXXX.jpg (UNDESCORE + WHATEVER + ".jpg") then you could explode() the string, remake it until the very last part of the array, and replace it
To rename a file on your server, use the rename() function (docs).
Example:
#Prefix is everything before the number. 0 is the start, -7 counts from the end
$prefix = substr( $url, 0, -7 );
if( rename( $url, $prefix . '800.jpg' ) ) {
#Yay! We renamed...
}
See rename and substr.
you can use the rename function in php , here is the link for it in the manual :
PHP Manual
and here is an example :
rename('sample.jpg' , 'sample2.jpg');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this line of PHP code here
<?php
file_put_contents('query.txt', parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
?>
Whenever
phpfile.php?blablabla
is queried it writes a query.txt with the parameter in it, in this case, blablabla.
BUT, when I do this, it deletes the past query.txt file and writes a totally new one.
I want the queries to be ADDED into the .txt file. So there can be as many queries and possible and every single value entered will be lined up in the .txt file..
For example
I want it so phpfile.php?test is visited, query.txt looks like this
test
after that, phpfile.php?test2 is visited
now query.txt looks like this
test
test2
and this goes on forever.
How do I do this?
p.s.: sorry, I'm totally a Java type. I'm an absolute beginner to PHP.
You wish to append to the file, so you should use something along the lines of
$handle = fopen("myfile.txt", "a");
fwrite ($handle, parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
fclose($handle);
You can find all the other read/write file open flags at the docs.
http://php.net/manual/en/function.fopen.php
It appears you can also just pass a flag with your existing code, as that will abstract the file handle open/close from you;
file_put_contents('query.txt', parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), FILE_APPEND);
Add FILE_APPEND as 3rd argument to file_put_contents(). Example (.PHP_EOL to add new line):
$data = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY).PHP_EOL;
file_put_contents('query.txt', $data, FILE_APPEND);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to create an CMS-system where I can add photos to my website, and for the photo-part, I want to have the ability to enter which programs I had used to create that photo.
I should be possible to add more than one program.
Right now I am saving the programs in the database like this:
In the programs-row:
photoshop illistrator indesign
Then at my website, it could be nice if the icons/logos of the used programs, could show up next to the photo.
So my question is how to do create a new div, what a new class, based on the words from the programs-row?
Fx:
photoshop illustrator indesign
Becomes to:
<div class="photoshop"></div>
<div class="illustrator"></div>
<div class="indesign"></div>
Hope you guys can help me with this problem :)
Thanks ;)
Use the explode function and a foreach loop to perform the string manipulation.
$programs = explode(" ", $data);
foreach($programs as $value) {
//Echo out the html - $value contains the program name
}
I leave it to you to figure out how to format the program name with the HTML that you need.
$fx = "photoshop illistrator indesign";
$words = explode(" ", $fx);
array_walk($words, function(&$n) {
echo '<div class="'.$n.'"></div>';
});