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');
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
i have a file in a directory named 'achieve3000_Gestalt_students102713.txt'.. i want to reduce the file name to Gestalt_students ONLY
Have tried using explode() which gives something different altogether and preg replace
Please help
EDIT
i have 4 files
achieve3000_Gestalt_students102713.txt
achieve3000_Gestalt_teachers102713.txt
achieve3000_Perspectives_Middle_Academy_students102713.txt
achieve3000_Perspectives_Middle_Academy_teachers100913.txt
If the file name always begins with a character sequence whose length is equal to the length of "achieve3000_" (which is 12) and the number at the end is always 6 digits plus a 4 character extension sequence. The you can use this code
$output = substr($input, 12, strlen($input) - 22);
var_dump(substr('achieve3000_Gestalt_students102713.txt', 12, 16));
Did you try substr?
Like :
$input = "achieve3000_Gestalt_students102713.txt";
$answer =substr ($input, 12, 16);
echo $answer;
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
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.
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 an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}