PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Dec 17, 2017 12:33 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Jul 11, 2014 4:36 am 
Offline
Forum Newbie

Joined: Fri Jul 11, 2014 4:26 am
Posts: 2
hi people

I'm new with regex in php and need help to build a regex

from a string like
xx_yy_zz_ww.php
I should get a string like
xx_yy_hh_ww.php
the components of the string are separated by underscores

hh xx yy zz are numbers and they have at least one digit, while ww is a text, a file name

so I should replace zz with hh, zz is the number in 3th position from left

thanks for helping me


Top
 Profile  
 
PostPosted: Fri Jul 11, 2014 5:38 am 
Offline
Forum Newbie

Joined: Fri Jul 11, 2014 4:26 am
Posts: 2
I realized that I can simply explode and implode the string with '_' separator


Top
 Profile  
 
PostPosted: Sun Sep 28, 2014 2:19 am 
Offline
Forum Newbie

Joined: Tue Aug 12, 2014 6:13 am
Posts: 19
Location: Chennai, India
CarlosMS wrote:
...............................
so I should replace zz with hh, zz is the number in 3th position from left

.....................


One way could be

Syntax: [ Download ] [ Hide ]
$s = "12_1_22_ww.php";
$g = preg_replace('/([0-9]+_[0-9]+_)([0-9]+)(_[0-9a-z\.]+)$/i', '${1}33${3}' , $s);
print $g;

_________________
Chris, Php Developer and Programmer,
https://www.phpfreelanceprogrammer.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group