简体   繁体   English

PHP删除链接中的文本

[英]PHP removing text in a link

How do you strip/remove wording in PHP? 如何在PHP中删除/删除措辞?

I have a form that's passing a full URL link to an output page. 我有一个将完整的URL链接传递到输出页面的表单。

Example: maps/africa.pdf 示例: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. 在输出页面上,我想提供一个“ href链接”,但是在PHP中使用相同的发布URL,但是去掉“地图”并提供一个仅显示非洲的链接。

Example: africa 示例:非洲

can this be done? 可以做到吗?

Thanks! 谢谢!

Use pathinfo : 使用pathinfo

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

If you want only .pdf to be stripped, use basename : 如果只希望剥离.pdf ,请使用basename

$filename = 'maps/africa.pdf';
$title = basename($filename, '.pdf');
$string = 'maps/africa.pdf';

$link_title = str_replace(array('maps/', '.pdf'), '', $string);

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];
}

Some good answers here. 这里有一些好的答案。 Also, if you know the url every time you could count the characters and use substr() eg http://uk3.php.net/substr 另外,如果您每次都知道网址,则可以计算字符数并使用substr(),例如http://uk3.php.net/substr

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM