• I’ve got the following switch statement which works well.

    switch ($currenturl_relative) {
        
            case '/devel/lms/ja/lessons/planning-a-meeting-int/':
                $urlto = home_url('/lessons/planning-a-meeting-int/');
                break;
                
            case '/devel/lms/ja/quizzes/planning-a-meeting-int-quiz/':
                $urlto = home_url('/quizzes/planning-a-meeting-int-quiz/');
                break;    
            
            default:
                return;
        
        }

    But I’d like to set this up so it works in a “wild card” fashion.
    Something like:

    switch ($currenturl_relative) {
        
            case '/devel/lms/ja/lessons/*/':
                $urlto = home_url('/lessons/*/');
                break;
                
            case '/devel/lms/ja/quizzes/*/':
                $urlto = home_url('/quizzes/*/');
                break;    
            
            default:
                return;
        
        }

    But of course “*” (asterisk) is NOT the correct syntax.
    Any advice on how to extract the final URL parameter as a variable that can be just plugged into the redirection case switcher?
    A rookie PHP question to be sure 😉

    • This topic was modified 6 years, 6 months ago by TWD.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    You can use regex for do that, something like this return the * value:

    <?php
    
    $string = '/devel/lms/ja/lessons/planning-a-meeting-int/';
    
    $out = '';
    
    preg_match( '/\/devel\/lms\/ja\/lessons\/(.*)\//', $string, $out );
    
    var_dump( $out );
    Thread Starter TWD

    (@twd)

    Thanks but I think you kind of missed my point.

    $string = '/devel/lms/ja/lessons/planning-a-meeting-int/';

    I want something thats going to parse out ALL urls that begin: /devel/lms/ja/lessons/[slug]/

    and return them as:
    /lessons/[slug]/

    Hence the question about “wild card” parameters.

    For all url start with /devel/lms/ja/lessons/[slug]/

    <?php
    
    $tests_url = array();
    $tests_url[0] = '/devel/lms/ja/lessons/planning-a-meeting-int/';
    $tests_url[1] = '/devel/lms/ja/lessons/test/';
    
    $out = '';
    
    foreach ( $tests_url as $test_url ) {
       preg_match( '/\/devel\/lms\/ja\/(lessons)\/(.*)\//', $test_url, $out );
       var_dump( $out );
    }

    For the first entry of tests_url: /devel/lms/ja/lessons/planning-a-meeting-int/

    $out[0] = “/devel/lms/ja/lessons/planning-a-meeting-int/”
    $out[1] = “lessons”
    $out[2] = “planning-a-meeting-int”

    For the second entry of tests_url: /devel/lms/ja/lessons/test/

    $out[0] = “/devel/lms/ja/lessons/test/”
    $out[1] = “lessons”
    $out[2] = “test”

    <?php
    
    //$string = '/devel/lms/ja/lessons/';
    $string = '/devel/lms/ja/lessons/planning-a-meeting-int/';
    
    preg_match_all( '|^/devel/lms/ja/([^/]+)/([^/]*)/$|', $string, $out );
    
    if(!empty($out[1]))
    {
    $out[0] = $out[1][0];
    $out[1] = $out[2][0];
    unset($out[2]);
    // Without start and end slash
    var_dump( $out );
    }

    leassons or quizzle is first slug

    If $currenturl_relative returns the path with two slugs you can also temporarily insert a variable containing only the two slugs.

    <?php
    $currenturl_relative = '/devel/lms/ja/lessons/planning-a-meeting-int/';
    $start_url = dirname(dirname($currenturl_relative)).'/';
    $slug_all = substr($currenturl_relative,strlen($start_url));
    switch ($start_url) {
    case '/devel/lms/ja/';
    preg_match( '|^([^/]+)+/([^/]+)+/$|', $slug_all, $out );
    
    if(!empty($out))
    {
    $out[0] = $out[1];
    $out[1] = $out[2];
    unset($out[2]);
    // Without start and end slash
    var_dump( $out );
    }
    break;
    
    default:
    return;
    }

    Fix with preg_match…

    
    <?php
    //$string = '/devel/lms/ja/lessons/';
    $string = '/devel/lms/ja/lessons/planning-a-meeting-int/';
    
    preg_match( '|^/devel/lms/ja/([^/]+)+/([^/]+)+/$|', $string, $out );
    
    if(!empty($out))
    {
    $out[0] = $out[1];
    $out[1] = $out[2];
    unset($out[2]);
    // Without start and end slash
    var_dump( $out );
    }
    • This reply was modified 6 years, 6 months ago by autotutorial.
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Hows to create a redirection “wildcard” function’ is closed to new replies.