tags:

views:

88

answers:

4

How do you strip/remove wording in PHP?

I have a form that's passing a full URL link to an output page.

Example: maps/africa.pdf

And on the output page, I want to provide an "href link", but in PHP use that same posted URL, but strip off the "maps" and have it provide a link that just says africa.

Example: africa

can this be done?

Thanks!

+2  A: 
$string = 'maps/africa.pdf';

$link_title = str_replace(array('maps/', '.pdf'), '', $string);
David Caunt
A: 

So you just want the file name? If so, then that would be everything between the last slash and the last dot.

if (preg_match("@/([^/]+)\\.[^\\./]+$@", $href, $matches)) {
    $linkText = $matches[1];
}
nickf
+9  A: 

Use pathinfo:

$filename = 'maps/africa.pdf';
$title = pathinfo($filename, PATHINFO_FILENAME);

If you want only .pdf to be stripped, use basename:

$filename = 'maps/africa.pdf';
$title = basename($filename, '.pdf');
strager
duh, of course... this is a much simpler solution than using a regex. +1
nickf
A: 

Some good answers here. Also, if you know the url every time you could count the characters and use substr() e.g. http://uk3.php.net/substr

$rest = substr("abcdef", 2, -1); // returns "cde"

jsims281