0

I visit my page www.mysite.com/theme/test/index.php which contains the code below. I expect to see the word test inside the h1 but it is blank :-( Can someone explain where I've gont wrong?

<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if ((false !== strpos($url,'theme')) || (false !== strpos($url,'colours'))) {

    if(preg_match("/\/(\d+)$/",$url,$matches)) { $end=$matches[1]; }
    ?>
    <div class="row">
        <div class="medium-12 columns">
            <h1 class="page-title"><?php echo ucwords(str_replace("-"," ",$end)); ?></h1>
        </div>
    </div>
<?php
}
?>

Thank you

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190

3 Answers3

2

I think your preg_match is wrong. \d means digit. You don't have any in your url.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

I'm not really sure exactly what information you have going through your script when it is being run, but my best guess is that $end is never being set.

Check to make sure that your regular expression matches, otherwise $end will be empty.

Steven Jeffries
  • 3,522
  • 2
  • 20
  • 35
0

Resolved.

@Marcin was right.

$end = end((explode('/', rtrim($url, '/'))));

Found this useful: Get Last Part of URL PHP

Community
  • 1
  • 1
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190