?
Solved

Regular expression question

Posted on 2006-05-04
3
Medium Priority
?
165 Views
Last Modified: 2006-11-18
Hi,

$str1 = 'some_text_1_' . time() . '_end';
$str2 = 'some_text_103_' . time() . '_end2';

With a regular expression, I want to replace the *value* of time() in $str1 and $str2 with some other string. For example, I want the result as:

$str1 = 'some_text_1_NEW_end';
$str2 = 'some_text_103_NEW2_end2';

Please help.
0
Comment
Question by:karthikramanathan
3 Comments
 

Author Comment

by:karthikramanathan
ID: 16605844
In short, I need a regular expression for this string:

'some_text_*_#_%';

where:

 * denotes a numeric entry,
 # denotes the value of time(), and
 % is some string

Neither of these symbols can be empty. I want to replace the # with some string. The result should be:

'some_text_*_NEW_%';
0
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 100 total points
ID: 16606281
How about this:
<?php
$str1 = 'some_text_1_' . time() . '_end';
$replacement = 'NEW';
$str1 = preg_replace('/(.*\d+_)(\d+)(.*)/', '$1'.$replacement.'$3', $str1);
print($str1);
?>
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 400 total points
ID: 16609039
<?php
  $str1 = 'some_text_1_' . time() . '_end';
  $str2 = 'some_text_103_' . time() . '_end2';
  $preg = '/(some_text_\d+_)(\d+)(_\w+)/i';
  $repl = 'NEW2';
 
  echo '<p>'.preg_replace($preg, "\\1$repl\\2", $str1).'</p>';
  echo '<p>'.preg_replace($preg, "\\1$repl\\2", $str2).'</p>';
?>
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question