Solved

Need to use regexp to change a variable.. Pls help!

Posted on 2006-10-29
3
178 Views
Last Modified: 2006-12-05
I'm using a filename in a variable, but ned to dynamically change the variable within the page. For example, if my filename is mainpage.htm, then I want the result to be mainpage_rt.htm. It won't always be an htm page, but it will always have an extension. Please help. THanks in advance for all the help.
0
Comment
Question by:bemara57
3 Comments
 
LVL 4

Accepted Solution

by:
KarlPurkhardt earned 500 total points
ID: 17829210
Hi,

Try the following regex:

$filename = 'somepage.html';
$add_string = '_r';

$filename = preg_replace('/(.*)(\.\w{2,5})/i', "$1$add_string$2", $filename);
      
echo 'Filename is now: ' . $filename;
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

786 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