onewaydom Posted October 9, 2007 Share Posted October 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/ Share on other sites More sharing options...
effigy Posted October 9, 2007 Share Posted October 9, 2007 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. 1. /\A(.*?)(?=string)/s 2. /\Apattern/ 3. /pattern\z/ 4. /\s*\z/ replaced with a new line. 5. A string containing multiple lines? Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365599 Share on other sites More sharing options...
onewaydom Posted October 9, 2007 Author Share Posted October 9, 2007 5. A string containing multiple lines? A long list of data that needs to be adjusted. Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365610 Share on other sites More sharing options...
effigy Posted October 9, 2007 Share Posted October 9, 2007 That sounds like the same thing to me, unless you're looping through a file line by line rather than slurping it all into one string. In either case, you're still using preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365614 Share on other sites More sharing options...
onewaydom Posted October 9, 2007 Author Share Posted October 9, 2007 How would I setup 'preg_replace'? Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365622 Share on other sites More sharing options...
effigy Posted October 9, 2007 Share Posted October 9, 2007 preg_replace Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365625 Share on other sites More sharing options...
onewaydom Posted October 9, 2007 Author Share Posted October 9, 2007 BIG HELP Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365626 Share on other sites More sharing options...
onewaydom Posted October 9, 2007 Author Share Posted October 9, 2007 That sounds like the same thing to me, unless you're looping through a file line by line rather than slurping it all into one string. In either case, you're still using preg_replace. I have the initial prefix and suffix being adjusted properly, but when I have more than one occurrence the function only takes care of the beginning and the end. $second = preg_replace(" /\A(.*?)($oldprefix?)/s", "$newprefix", "$content"); $third = preg_replace(" /$oldsuffix\z/", "$newsuffix", "$second"); I would like it to make a new line after each new suffix is added. Additionally be able to do multiple lines of similar, but different text. A long list if you will. Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365649 Share on other sites More sharing options...
effigy Posted October 9, 2007 Share Posted October 9, 2007 In order to handle multiple lines, change \A to ^, and add the /m modifier at the end, e.g. /^(.*?)($oldprefix)/sm. Keep in mind that if you use a variable within a regular expressions, it must first be passed through preg_quote. Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365656 Share on other sites More sharing options...
onewaydom Posted October 9, 2007 Author Share Posted October 9, 2007 Not sure if this is what you were meaning, but this works. $content = trim( "$content"); $oldprefix = preg_quote($oldprefix, '/'); $second = preg_replace(" /^(.*?)($oldprefix?)/sm", "$newprefix", "$content"); $third= str_replace("$oldprefix","$newprefix",$second); $fourth = str_replace("$oldsuffix","$newsuffix <br>",$third); Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365669 Share on other sites More sharing options...
effigy Posted October 9, 2007 Share Posted October 9, 2007 Why are you using ? after $oldprefix? FYI: You don't have to double quotes variable names unless they're being combined with other literals or strings. Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365671 Share on other sites More sharing options...
onewaydom Posted October 10, 2007 Author Share Posted October 10, 2007 Cool, thank you for the info. I have something very useful now! Quote Link to comment https://forums.phpfreaks.com/topic/72493-parsing-text/#findComment-365873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.