?
Solved

php regex and string op. help!

Posted on 2010-01-11
10
Medium Priority
?
512 Views
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.

thanx!
0
Comment
Question by:hathehariken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 14

Accepted Solution

by:
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.
0
 
LVL 4

Assisted Solution

by:sudhakarsp06
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);
0
 
LVL 6

Assisted Solution

by:magedroshdy
magedroshdy earned 100 total points
ID: 26282424
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
var_dump($_SERVER);

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

// TWO USEFUL THINGS FOR APPLICATION DEVELOPMENT
error_reporting(E_ALL);
echo "<pre>\n";



// PRINT THE SERVER NAME AND URL ADDRESS
echo $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];

Open in new window

0
 
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
0
 
LVL 111

Assisted Solution

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

Expert Comment

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

http://localhost/00/website/page1.php

use

./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.
0
 
LVL 12

Author Comment

by:hathehariken
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.

thanx,
hathehariken.
0
 
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
0
 
LVL 12

Author Comment

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

so...

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!
hathehariken.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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