Hello,
I am new to regular expressions.
Problem:
1. Remove all text before a certain string.
2. Replace something in the beginning string.
3. Replace something in the end of a string.
4. Be able to add a break after the end of each string.
5. Have this work on long strings repeating itself.
Example:
Dealing with large text files as long directories.
i.e. onebiglongdirectory/prefix/somethingelsehere/suffix
onebiglongdirectory/prefix/somethingelsehere/suffix
onebiglongdirectory/prefix/somethingelsehere/suffix
Of course with space and other unnecessary text also included.
Solution:
1. I would like everything before the prefix dropped and replaced with a value that I input into a variable.
2. I would like the suffix dropped and replaced with a variable that I input as a variable.
3. Each directory on its own line.
Here is something I have created. It works a bit, but stops short of doing the whole string of directories I put into it.
index.php
<html>
<head>
</head>
<body bgcolor = "#ACACAC">
<center>
<h1>Regular Expressions Engine</h1>
<h5>Enter the prefix that is to be changed along with the suffix, both new and old.</h5>
<form method=post action="expressions.php">
New Prefix
<input type=text name="newprefix" size=40 style="background:#ECECEC"><br><br>
Old Prefix
<input type=text name="oldprefix" size=40 style="background:#ECECEC"><br><br>
New Suffix
<input type=text name="newsuffix" size=40 style="background:#ECECEC"><br><br>
Old Suffix
<input type=text name="oldsuffix" size=40 style="background:#ECECEC"><br><br>
Enter the string to be manipulated:<br><br>
<textarea name="content" rows=6 cols=40 style="background:#FFFDDA">
</textarea>
<br><br>
<input type=submit value="Process"> <input type="reset" value="Reset">
</form>
</center>
</body>
</html>
expressions.php
<?
$content = trim( "$content");
$firststring =strstr("$content", "$oldprefix");
$string= str_replace("$oldprefix","$newprefix",$firststring);
$second = str_replace("$oldsuffix","$newsuffix <br>",$string);
?>
<html>
<title>Regular Expressions Results</title>
<body bgcolor = "#ACACAC">
<center>
<h1>Results</h1>
<p><br><b></b></p>
<p>
<b><font color="#00A54E">
<?
echo $second;
?>
</b></font>
</p>
<br>
<br>
<br>
<a href ="http://www.mydomain.com/regex">Back</a>
</center>
</body>
</html>
Any help appreciated. I know the expressions I am looking for, but just don't know how to get them into php.
Replace oldsuffix to the end of the line with newsuffix
\.oldsuffix.*$ .newsuffix
Replace beginning of the line up to and including /oldprefix with /newprefix
^.*?/oldprefix
Hope this makes sense!
Thanks