56433

PHP Regexp on filename and number - preg_replace_callback

How to catch and filer the filename

I have tried in PHP using <strong>preg_replace_callback</strong>, which I dont how to use correctly.

function upcount_name_callback($matches) { //var_export($matches); $index = isset($matches[3]) ? intval($matches[3]) + 1 : 1; return '_' . $index; } $filename1 = 'news.jpg'; echo preg_replace_callback('/^(([^.]*?)(?:_([0-9]*))?)(?:\.|$)/', 'upcount_name_callback', $filename1, 1); $filename2 = 'aw_news_2.png'; echo preg_replace_callback('/^(([^.]*?)(?:_([0-9]*))?)(?:\.|$)/', 'upcount_name_callback', $filename2, 1);

<strong>Output (wrong):</strong>

array ( 0 => 'news.', 1 => 'news', 2 => 'news', 3 => '1', ) _1jpg <= wrong - filename1

.

array ( 0 => 'aw_news_2.', 1 => 'aw_news_2', 2 => 'aw_news', 3 => '2', ) _3png <= wrong - filename2

<strong>Output (aright):</strong>

news_1 <= filename1

.

aw_news_3 <= filename2

Answer1:

function my_replace_callback ($matches) { $index = isset ($matches [1]) ? $matches [1] + 1 : 1; return "_$index"; } $file = 'news.jpg'; $file = preg_replace_callback ('/(?:_([0-9]+))?\..*$/', 'my_replace_callback', $file); print ($file); $file = 'aw_news.jpg'; $file = preg_replace_callback ('/(?:_([0-9]+))?\..*$/', 'my_replace_callback', $file); print ($file); $file = 'news_4.jpg'; $file = preg_replace_callback ('/(?:_([0-9]+))?\..*$/', 'my_replace_callback', $file); print ($file); $file = 'aw_news_5.jpg'; $file = preg_replace_callback ('/(?:_([0-9]+))?\..*$/', 'my_replace_callback', $file); print ($file);

Answer2:

function upcount_name_callback($matches) { $index = isset($matches[3]) ? intval($matches[3]) + 1 : 1; return $matches[2] . '_' . $index; } $filename1 = 'news.jpg'; echo preg_replace_callback('/^(([^.]*?)(?:_([0-9]*))?)(?:(\..*)|$)/', 'upcount_name_callback', $filename1); $filename2 = 'aw_news_2.png'; echo preg_replace_callback('/^(([^.]*?)(?:_([0-9]*))?)(?:(\..*)|$)/', 'upcount_name_callback', $filename2);

Recommend

  • Count values in $_POST
  • Combine multiple array to another array [duplicate]
  • File-driven unit tests in Visual Studio?
  • Getting intersection of two lists in python
  • Laravels Eloquent ORM: setting datatypes of the model
  • How to merge two sets of weka Instances together
  • Why is git not able to merge?
  • Do I need to clean user input for DB::query calls in laravel?
  • How to execute a shell script through python
  • Does pImpl fundamentally solve C++ DLL issue?
  • How to activate JS data-filter when page loads?
  • How should I start to implement RESTful web service?
  • PHP file_exists() anomaly
  • array_search() in Session Array
  • MVVM: Image Bind Source from FileOpenPicker
  • Divide a $1 by 3 and adjusting 1 cent
  • Google Places API - Find a company's CID and LRD
  • How to use function wrapper in mustache.php?
  • Hide HTML elements without javascript, only CSS
  • How does this usort cmp function actually work?
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • How to have background script and something similar to a default popup?
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Syntax for setting draggablecursor property in google maps api
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Regex thinks I'm nesting, but I'm not
  • PHP - How to update data to MySQL when click a radio button
  • Resize panoramic image to fixed size
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • php design question - will a Helper help here?
  • Proper way to use connect-multiparty with express.js?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?