Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/235069-working-with-preg_replace/
Share on other sites

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);
            }

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.

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.

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);
            }

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

 

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.

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.

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);
            }
            ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.