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.