473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem with regular expressions following code snippet...

I'm definately not a regexp wiz.... need some suggestions here:

grab PHP_SELF, strip the end filename off and the 2 directories
preceeding it...

This is what I've come up with so far, except I'd like to replace the
..html for any extension and I've tried it but doesn't work... and I've
also tried parsing backing a second directory but no luck, maybe you
have an alternative solution:

--- snip ---

What I have tried:

// retrieves URL of itself, then strips the directories only leaving
the filename
$PHP_SELF = $_SERVER['PHP_SELF'];
$new = preg_replace("/[^\/](.*?)[$\/]/mis",'',$PHP_SELF);
$new = preg_replace("/\//mis",'',$new);

// replace the file only so we have the directory and append the
contact form onto it
$expression =
sprintf("/[$\/](?=...)([a-z0-9]+.\.[a-z]+)/mi",$new); // or

$expression = sprintf("/[$\/](?=...)([a-z0-9]+.\.htm?)/mi",$new); or

$expression =
sprintf("/[$\/](?=...)([a-z0-9]+.\.htm?)(?<=...)(-[a-z][\/])/mi",$new);
$contactform = preg_replace($expression,'',$PHP_SELF);
$contactform = $contactform.'/contact_form1.html';
What I have now:

// retrieves URL of itself, then strips the directories only leaving
the filename
$PHP_SELF = $_SERVER['PHP_SELF'];
$new = preg_replace("/[^\/](.*?)[$\/]/mis",'',$PHP_SELF);
$new = preg_replace("/\//mis",'',$new);

// replace the file only so we have the directory and append the
contact form onto it
$expression = sprintf("/[$\/](?=...)%s/mi",$new);
$contactform = preg_replace($expression,'',$PHP_SELF);
$contactform = $contactform.'/contact_form1.html';

--- snip ---

PHP_SELF = /dirname1/dirname2/dirname3/dirname4/somefile.html

which generally results in
/dirname1/dirname2/dirname3/contact_form1.html

but what I would like is to also remove dirname3 as well resulting in:

/dirname1/dirname2/contact_form1.html

Jul 17 '05 #1
2 1612
Anyone out there know regex???

Jul 17 '05 #2
Ok, after a little more searching, a little more clearer thinking I
sovled it with:

/[\/]\w+[\/](\w+\.[a-z]{3,4})/mi

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: | last post by:
I'm trying to scan a (binary) file for a string matching a particular pattern, and am getting unexpected results. I don't know if this is a bug or just my own misunderstanding of regular...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Steve | last post by:
Hi all I have to validate a password to determine whether or not it adheres to certain rules. For example, the password must contain at least 1 number, at least 1 uppercase character and at...
4
by: Arne | last post by:
Hi, I'm trying to recognise phone numbers on the following format using regular expressions: +47 123 45 678 This should be recognised by the following regular expression: +47 \d{3} \d{2}...
4
by: Ben Dewey | last post by:
Hey, I have only been playing with regular expressions for some time. I am working on some code that parses and object 560 event log. I have created two expressions the first one which works...
1
by: Daniel Walzenbach | last post by:
Hi, does anybody know I can extract a substring of a text with regular expressions. Let’s consider the following text: “Regular expressions are often used to make sure that a string matches a...
5
by: Greg Vereschagin | last post by:
I'm trying to figure out a regular expression that will match the innermost tag and the contents in between. Specifically, the string that I am attempting to match looks as follows: ...
5
by: Kijak | last post by:
Hi, Im just starting working with REGEX and got a few problems. Could you tell me how to test if two strings can be found in another. ei: String to test: "This is a great car you got" Look...
1
by: Engineerik | last post by:
According to what I see in the manuals I should be able to use regular expressions with the rowfilter property of a dataview but I keep getting errors. Example: I want to find any rowview where...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.