Reputation:
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!
Upvotes: 0
Views: 193
Reputation: 2206
Some good answers here. Also, if you know the url every time you could count the characters and use substr() e.g. https://www.php.net/substr
$rest = substr("abcdef", 2, -1); // returns "cde"
Upvotes: 0
Reputation: 90022
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');
Upvotes: 9
Reputation: 546075
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];
}
Upvotes: 0
Reputation: 58361
$string = 'maps/africa.pdf';
$link_title = str_replace(array('maps/', '.pdf'), '', $string);
Upvotes: 2