Solved

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

Posted on 2006-10-29
3
177 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to dynamically set the form action using jQuery.

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now