php regex and string op. help!

Posted on 2010-01-11
Medium Priority
Last Modified: 2013-12-13
hello there!

i need some help with some regular expressions and some string operations.
i am new to php, and cant manage to get my head around the construction of regexes...

if you can solve this problem i would be most happy.
if you can explain the process how you solved it, i would be more than most happy.

$localsource1 = "http://localhost/00/website/page1.php";
$localsource2 = "http://localhost/00/website/page2.php?fn=3&con=1U1q1I1P1PfZXgl";

at the moment i am working in a local server, but this will go to a production server later on, so the URLs would change to:

$prodsource1 = "http://www.website.com/page1.php";
$prodsource2 = "http://www.website.com/page2.php?fn=3&con=1U1q1I1P1PfZXgl";

what i need the regular expression(s) to do is truncate the two strings to this:

$localsource1 = "website/page1.php";
$localsource2 = "website/page2.php";
$prodsource1 = "website.com/page1.php";
$prodsource2 = "website.com/page2.php";

search for the word "website" and delete all left side characters
then search for the word "php" and delete all right side characters.
keep the middle and pass on.

Question by:hathehariken
LVL 14

Accepted Solution

svgmuc earned 1600 total points
ID: 26282379
$localsource1 = preg_replace('/^.*website\//', 'website/', $localsource1);
$localsource2 = preg_replace('/^.*website\//', 'website/', $localsource2);
$localsource2 = preg_replace('/\?.*$/', '', $localsource2);

$prodsource1 = preg_replace('/^.*website\.com\//', 'website.com/', $prodsource1);
$prodsource2 = preg_replace('/^.*website\.com\//', 'website.com/', $prodsource2);
$prodsource2 = preg_replace('/\?.*$/', '', $prodsource2);

There's probably better a way by counting the slashes but this one will do for now.

Assisted Solution

sudhakarsp06 earned 100 total points
ID: 26282384
Here is the code

$str = "http://localhost/00/website/page2.php?fn=3&con=1U1q1I1P1PfZXgl";
$exp = '!'.'http:\/\/(.*)\/website'.'!';
$exp3 = '!'.'(.*)\?(.*)'.'!';
$str_rep = preg_replace($exp,'website',$str);
$replace = '$1';
//echo $str_rep = preg_replace($exp3,$replace,$str_rep);

$str2 = "http://www.website.com/page2.php?fn=3&con=1U1q1I1P1PfZXgl";
$exp2 = '!'.'http:\/\/www\.(.*)\/(.*)'.'!';
$replace2 = '$1/$2';
$str_rep2 = preg_replace($exp2,$replace2,$str2);
echo$str_rep2 = preg_replace($exp3,$replace,$str_rep2);

Assisted Solution

magedroshdy earned 100 total points
ID: 26282424
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 26282543
Where you get these URL fields from may determine the way you want to process this information.  Most PHP installations would prepopulate the $_SERVER array with this data.  So you might not need any REGEX at all.

You can print out the _SERVER information with this statement:

echo "<pre>"; // MAKES IT EASIER TO READ

If this information is in the _SERVER array, this code snippet may work just fine.  You can install it and run it on your localhost and on your server - see what you get.

If you want to learn more about REGEX, get a copy of "The Regex Coach" - google it.  Very useful!

Best regards, ~Ray
<?php // RAY_url.php

echo "<pre>\n";


Open in new window

LVL 111

Expert Comment

by:Ray Paseur
ID: 26282580
Sedebar note regarding this:

"cant manage to get my head around the construction of regexes"

Ha!  Nobody can!  There are whole books written about the subject and the WWW is full of examples and tutorials that are held out as good examples but are simply wrong in one way or another, so when you grab an example from some place, caveat emptor - test thoroughly.

Often you can achieve the desired effects with simpler code, such as a state engine, or through the use of the explode() function.

Best of luck with your project, ~Ray
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 26283269
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26283604
Why not avod the REGEX by avoiding absolute URLs in the code? Instead of



./page1.php (or ./00/website/page1.php if you are at the top level of the folder structure) and the links will resolve themselves correctly whichever server they are on.
LVL 12

Author Comment

ID: 26284306
thank you all for posting!

this string is a way of authentication of the referer of the previous page.

the only valid way to come to page 3 is via page 2.
again the only valid way to come to page 2 is via page 1a, 1b, and 1c.

this code, sitting on page 3, will determine how you came, and what time you took.
the "con" variable in the URL contains the previous referer in an encrypted form.

i cant use cookies or sessions.

i will use the regex codes you posted and reply i a few hours time.

LVL 111

Expert Comment

by:Ray Paseur
ID: 26284671
So is what you want to get the value of the con variable?  That would be in $_GET["con"] and of course it would be visible to the client and almost certainly cached on the client browser, so a "back" button would find it.  If what you want is the URL without the /GET string, that is in PHP_SELF
LVL 12

Author Comment

ID: 26326954
returned today, and found an "abandoned question" in my inbox.
so here i am, all present and accounted for :)


svgmuc, thanks for your solution, it works perfectly.

magedroshdy, the basename() command, did not output the thing i wanted, but your link provides some very useful knowledge i didnt know, thank you

sudhakarsp06, worked perfectly, but too long

Ray, why dont they make regex simpler?? haha! and thank you for the links, they are quite informative, i really liked the cheatsheet.

bportlock, completely out of context, but useful thing to know.
i normally put all my main files in the root, with all includes and require_onces in a separate folder so normally dont require the use of such paths.

i'll split among all replies.
thanks for the help!

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
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…
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

615 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