RopeADope Posted April 29, 2011 Share Posted April 29, 2011 Hi all. I'm having a bit of an issue with preg_replace and I'm not sure how to solve it. I'm having preg_replace find a "bus" (short for business) prefix in a file name and then print "Business Management" instead of "bus". I also have another prefix of "bus_it_requests". When preg_replace finds that prefix, it prints "Business Management It Requests" but I need it to print "Business It Requests". How do I have preg_replace make the distinction in the prefix matches? Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/ Share on other sites More sharing options...
cyberRobot Posted April 29, 2011 Share Posted April 29, 2011 You should be able to run the preg_replace for "bus_it_requests" first which would remove all the references. Then run the preg_replace for "bus". Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208097 Share on other sites More sharing options...
RopeADope Posted April 29, 2011 Author Share Posted April 29, 2011 No such luck. Here's the bit of code I have doing it...maybe I missed something. if ($handle = opendir($_SERVER['DOCUMENT_ROOT'] . '/documents/context/')) { echo "Files:</br>"; while (false !== ($file = readdir($handle))) { if($file=="." or $file==".."){ echo ""; }else{ $pattern=array("/.pdf/","/_/","/sop/i","/mgmt/i","/ste/i","/risk/i","/eng/i","/dem/i","/bus_it_requests/i","/bus/"); $replace=array(""," ","SOP","Management","Steady State -","Risk Management -","Engineering -","Demand Management -","Business/IT Requests","Business Management -"); echo "<a href=\"$file\">" . ucwords(preg_replace($pattern,$replace,$file)) . "</a></br>"; } } closedir($handle); } Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208103 Share on other sites More sharing options...
cyberRobot Posted April 29, 2011 Share Posted April 29, 2011 To clarify, I meant that you should be able to run preg_replace twice. The first would handle the "bus_it_requests" part. Then you would call preg_replace again to handle everything else. Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208109 Share on other sites More sharing options...
RopeADope Posted April 29, 2011 Author Share Posted April 29, 2011 I could. It just seemed redundant if there was a method for doing it in one preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208110 Share on other sites More sharing options...
cyberRobot Posted April 29, 2011 Share Posted April 29, 2011 Is there something around the keywords you're replacing that could be used? For example, in the original string is the word "bus" followed by a space? If so, you could modify the pattern to "/bus /". Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208113 Share on other sites More sharing options...
mattal999 Posted April 29, 2011 Share Posted April 29, 2011 Could you not just use str_ireplace? $pattern=array(".pdf","_","sop","mgmt","ste","risk","eng","dem","bus_it_requests","bus"); $replace=array(""," ","SOP","Management","Steady State -","Risk Management -","Engineering -","Demand Management -","Business/IT Requests","Business Management -"); echo "<a href=\"$file\">" . ucwords(str_ireplace($pattern,$replace,$file)) . "</a></br>"; Just a thought - it depends on how str_replace works as to whether it'll solve your issue. If you need some case sensitive, then just do the case insensitive one and then the case sensitive one on the result. Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208114 Share on other sites More sharing options...
cyberRobot Posted April 29, 2011 Share Posted April 29, 2011 I experimented with the code and it looks like the issue is coming from the "/_/" pattern near the beginning. Since this removes the underscores, the following pattern "/bus_it_requests/i" doesn't do anything. Moving the underscore pattern to the end seems to work for me. Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208121 Share on other sites More sharing options...
RopeADope Posted April 29, 2011 Author Share Posted April 29, 2011 Is there something around the keywords you're replacing that could be used? For example, in the original string is the word "bus" followed by a space? If so, you could modify the pattern to "/bus /". This got me a bit closer to a solution. Now it prints "Bus It Requests" but I think its because its replacing a match twice. Updated function: while (false !== ($file = readdir($handle))) { if($file=="." or $file==".."){ echo ""; }else{ $pattern=array("/.pdf/","/_/","/sop/","/mgmt/","/ste/","/risk/","/eng/","/dem/","/bus_/","/_bus_it/"); $replace=array(""," ","SOP","Management","Steady State -","Risk Management -","Engineering -","Demand Management -","Business Management -","Business/IT Requests"); echo "<a href=\"$file\">" . ucwords(preg_replace($pattern,$replace,$file)) . "</a></br>"; } } closedir($handle); } Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208133 Share on other sites More sharing options...
cyberRobot Posted April 29, 2011 Share Posted April 29, 2011 Did you see Reply #7 about the underscore pattern? Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208134 Share on other sites More sharing options...
RopeADope Posted April 29, 2011 Author Share Posted April 29, 2011 Did you see Reply #7 about the underscore pattern? No, I missed that. Its still doing the same thing though. Its missing words and has some lower case stuff that should be upper case. I'm wondering if the list of replacements just needs to be reworked. Something like (replace prefix, replace suffix, remove underscores, remove extension). Here's what it prints out: Files: Business Management -financial Management Context Business Management -Management Context Business Management -service Level Management Context Business Management -service Portfolio Context Business Management -workforce Management Context Demand Management - Business Management -it Requests Context Demand Management - Management Context Demand Management - Management Change Management Context Demand Management - Project Management Context Demand Management - Release Management Context Engineering - Availability Management Context Engineering - Capacity Management Context Engineering - Configuration Management Context Engineering - Planning Context Engineering - Planning SOP Engineering - Problem Management Context Risk Management - Compliance Context Risk Management - Context Risk Management - Info Security Context Risk Management - Service Continuity Context Risk Management - Threat Management Context Steady State - Incident Management Context Steady State - Service Desk Context Steady State - Service Requests Context Steady State - Supplier Management Context Strategy Context Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208141 Share on other sites More sharing options...
cyberRobot Posted April 29, 2011 Share Posted April 29, 2011 It just seems like you need to rework the patterns. As with "bus_it_requests", you'll need to run all the patterns which start with "bus" before the "/bus/" pattern. For example, it looks like you have patterns like "bus_service" and "bus_financial". Those need to be moved. Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208147 Share on other sites More sharing options...
RopeADope Posted April 29, 2011 Author Share Posted April 29, 2011 It just seems like you need to rework the patterns. As with "bus_it_requests", you'll need to run all the patterns which start with "bus" before the "/bus/" pattern. For example, it looks like you have patterns like "bus_service" and "bus_financial". Those need to be moved. Right. I also think that maybe reworking the file naming convention may make this easier as well. The convention is [parent module]_[child module]_[type]. So bus_financial_mgmt_sop.pdf needs to be displayed as "Business Management - Financial Management - SOP". So if bus_financial_mgmt_sop.pdf was renamed to bus_mgmt_financial_mgmt_sop.pdf I'd just have to match "/bus_mgmt/","_financial_mgmt","_sop". I'll work on this today and post my solution. Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1208149 Share on other sites More sharing options...
RopeADope Posted May 4, 2011 Author Share Posted May 4, 2011 Finally got this figured out and working perfectly. I had to rework the file naming convention slightly but everything displays perfectly now. P.S. If there is any way to make this better or more compact, I'd greatly appreciate any input. <?php if ($handle = opendir($_SERVER['DOCUMENT_ROOT'] . '/documents/sop/')) { echo "Files:</br>"; while (false !== ($file = readdir($handle))) { if($file=="." or $file==".."){ echo ""; }else{ $pattern=array( "/bus_mgmt_/", "/dem_mgmt_/", "/eng_/", "/risk_mgmt_/", "/ste_sta_/", "/strategy_/", "/financial_mgmt_/", "/service_level_mgmt_/", "/service_portfolio_/", "/workforce_mgmt_/", "/bus_it_requests_/", "/change_mgmt_/", "/project_mgmt_/", "/release_mgmt_/", "/availability_mgmt_/", "/capacity_mgmt_/", "/configuration_mgmt_/", "/planning_/", "/problem_mgmt_/", "/compliance_/", "/info_security_/", "/service_continuity_/", "/threat_mgmt_/", "/incident_mgmt_/", "/service_desk_/", "/service_requests_/", "/supplier_mgmt_/", "/sop/", "/context/", "/.pdf/" ); $replace=array( "Business Management - ", "Demand Management - ", "Engineering - ", "Risk Management - ", "Steady State - ", "Strategy - ", "Financial Management - ", "Service Level Management - ", "Service Portfolio - ", "Workforce Management - ", "Business/IT Requests - ", "Change Management - ", "Project Management - ", "Release Management - ", "Availability Management - ", "Capacity Management - ", "Configuration Management - ", "Planning - ", "Problem Management - ", "Compliance - ", "Information Security - ", "Service Continuity - ", "Threat Management - ", "Incident Management - ", "Service Desk - ", "Service Requests - ", "Supplier Management - ", "SOP", "Context", "" ); echo "<a href=\"$file\">" . preg_replace($pattern,$replace,$file) . "</a></br>"; } } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/#findComment-1210341 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.